LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash function parameter (https://www.linuxquestions.org/questions/programming-9/bash-function-parameter-60280/)

Misel 05-17-2003 08:11 AM

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

Misel 05-17-2003 10:45 AM

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?

Hko 05-17-2003 11:51 AM

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.


All times are GMT -5. The time now is 07:41 AM.