LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2003, 08:11 AM   #1
Misel
Member
 
Registered: Mar 2003
Location: Berlin
Distribution: Slackware current
Posts: 310

Rep: Reputation: 31
Bash function parameter


Hello,

I try to make all my MP3s lowercase with a shell script. I know there are probably tons of solutions out there but I want to code it on my own.

I thought of a recursive function like this:

Code:
#!/bin/sh

function lowercase( $directory )
{
	for file in `ls $directory sed -e 's/ /%20/g'`;	# file list of each directory - %20 needed because ' ' is used as seperator
		do
			file=`echo $file | sed -e 's/%20/ /g'`;			#changes %20 back to spaces

			if [ -d $file ] then     #check if it's a directory
				do
					lowercase($file);
				done;
			#make it to lowercase here
		done
}

lowercase( "/mnt/windows/d/mp3s");
My problem is that the function parameter doesn't work. I don't know why and no paged I googled for mentioned function parameters in Bash. Do they even exist?

PS: I haven't gotten around to check for that lowercasing - but I would appreciate some hints there as well

Thank you in advance
-Misel
 
Old 05-17-2003, 10:45 AM   #2
Misel
Member
 
Registered: Mar 2003
Location: Berlin
Distribution: Slackware current
Posts: 310

Original Poster
Rep: Reputation: 31
okiedokie, I've managed it.

What had the programmer of Bash in his mind when he implemented the functions????

Code:
#!/bin/sh

function lowercase () {

if [ $1 ]
then

directory=$1;


for file in `ls $directory | sed -e 's/ /%20/g'`;	# file list of each directory - %20 needed because ' ' is used as seperator -i ignores case
do
	file=`echo $file | sed -e 's/%20/ /g'`;		#changes %20 back to spaces



	if [[ -d "$directory/$file" ]]  #if it is a directory change the names there first
	then
		echo $file;
		lowercase "$directory/$file";
	fi

	n=`echo $file | tr A-Z a-z`  #Change name to lowercase.

	if [[ $fname != $n ]]  # Rename only files not already lowercase.
	then
		echo  #$n lower case
		#mv $file $n
	fi
done
fi
}
Now I have another problem that I might solve before someone answers

directory is a global variable. but I need it to be private to the function. any ideas?
 
Old 05-17-2003, 11:51 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Qoute from the bash man page:

"Variables local to the function may be declared with the local builtin command. Ordinarily, variables and their values are shared between the function and its caller."

I guess, the reason you need a function-local variable, is because you are recursively wandering through the directories. You may find it nicer to have the "find" utility handle the recursive directory search. Something like:
Code:
#!/bin/sh
find -type f | while read FILE ; do
    mv "$FILE" "${FILE%/*}/`echo "${FILE##*/}" | tr A-Z a-z`" 2>/dev/null
done
Note that, by quoting properly, this does not need special handling of spaces. It's also not really needed to check if the file actually needs to be renamed: "mv" does that already, just directing the errors here it produces to /dev/null.

Last edited by Hko; 05-17-2003 at 11:52 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash parameter indirection possible? kornelix Programming 5 11-30-2005 08:35 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 10:02 AM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM
C: struct as a function parameter zokik Programming 3 12-06-2003 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration