LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with recursive bash script (https://www.linuxquestions.org/questions/programming-9/problem-with-recursive-bash-script-538706/)

me4linux 03-19-2007 08:24 AM

Problem with recursive bash script
 
Hi everyone

I have written recursive script to rename the files in the given folder such that the folder name is appended to the filename in the beginning.
I am getting following syntax error when I have run the script.I couldnt find any syntax error in it.

[linux@localhost myscripts]$ ./rename ./test
./rename: line 7: syntax error near unexpected token `else'
./rename: line 7: `else'
the script is as follows


# Name of the script is rename
#!/bin/sh
for FILE in $1
do

if [`file $FILE | grep directory`] then
echo $1 is a directory
./rename $FILE
else
echo $FILE is renamed to ${1}_${FILE}
mv $FILE ${1}_${FILE}
fi

done


can any one help to correct the mistake in this script

THANKS IN ADVANCE

bYe

macemoneta 03-19-2007 08:34 AM

Code:

if [`file $FILE | grep directory`] then
should be:

Code:

if [`file $FILE | grep directory`] ; then

nx5000 03-19-2007 08:41 AM

Code:

if [ -d $FILE ] ; then
echo "$FILE is a directory"
fi


omnio 03-19-2007 08:54 AM

Code:

#!/bin/sh

for FILE in $1/*
do
    if [ -d $FILE ] ; then
        echo $1 is a directory
        ./rename $FILE
    else
        echo $FILE is renamed to $(dirname $FILE)/$1_$(basename $FILE)
        mv $FILE $(dirname $FILE)/$1_$(basename $FILE)
    fi
done

and it is launched from outside the directory, like:
Code:

./rename test
EDIT: changed the "mv" destination and the "for" statement.

me4linux 03-19-2007 12:23 PM

[QUOTE=omnio]
Code:

    else
        echo $FILE is renamed to $(dirname $FILE)/$1_$(basename $FILE)
        mv $FILE $(dirname $FILE)/$1_$(basename $FILE)
    fi
done

hi omnio

can u explain me the above two lines ...
I didnt come across dirname and basename till now....
What mistake is there in my approach...
Code:

else
echo $FILE is renamed to ${1}_${FILE}
mv $FILE ${1}_${FILE}
fi

Please let me know the mistake in my code .......!!!

Sorry to say that script is not working ...

I have tried with the new code but its showing cannot rename
the file just under test directory is renamed...
but the files in the subdirectories are not getting renamed...
I tried using both ...i.e ./test and test as the command line arguments.....


[linux@localhost bash_script.tar.gz_FILES]$ ./rename ./test
./test is a directory
./test/1/f is renamed to ./test/1/./test/1_f
mv: cannot move `./test/1/f' to `./test/1/./test/1_f': No such file or directory
./test is a directory
./test/2/f is renamed to ./test/2/./test/2_f
mv: cannot move `./test/2/f' to `./test/2/./test/2_f': No such file or directory
./test/file is renamed to ./test/./test_file
[linux@localhost bash_script.tar.gz_FILES]$ ./rename test
test is a directory
test/1/f is renamed to test/1/test/1_f
mv: cannot move `test/1/f' to `test/1/test/1_f': No such file or directory
test is a directory
test/2/f is renamed to test/2/test/2_f
mv: cannot move `test/2/f' to `test/2/test/2_f': No such file or directory
test/file is renamed to test/test_fil

bye

cfaj 03-19-2007 03:36 PM

Quote:

Originally Posted by me4linux
I have written recursive script to rename the files in the given folder such that the folder name is appended to the filename in the beginning.

You mean prepended to the filename.
Quote:

I am getting following syntax error when I have run the script.I couldnt find any syntax error in it.

[linux@localhost myscripts]$ ./rename ./test
./rename: line 7: syntax error near unexpected token `else'
./rename: line 7: `else'
the script is as follows


# Name of the script is rename

It is not a good idea to use the name of a command that is included in your system.
Quote:

#!/bin/sh
for FILE in $1
do

if [`file $FILE | grep directory`] then
Code:

if [ -d "$FILE" ]; then
Quote:

echo $1 is a directory
./rename $FILE
This will produce an endless loop. If you call rename with the name of a directory, it will call itself again and again ...

Somewhere, you need to cd into the directory.
Quote:

else
echo $FILE is renamed to ${1}_${FILE}
mv $FILE ${1}_${FILE}
If $FILE contains path information, you will be asking to move the file into a probably non-existent directory.

If $FILE contains spaces (or other pathological characters), this will fail even if there is no path information. Quote your variables:
Code:

mv "$FILE" "${1}_${FILE}"
Quote:

fi

done


can any one help to correct the mistake in this script

Try this:
Code:

ren()
(
  cd "$1" || return
  for file in *
  do
    if [ -d "$file" ]
    then
      printf "%s is a directory\n"  "$file" >&2
      ren "$file"
      continue
    fi
    printf "%s is renamed to %s\n" "$file" "${PWD##*/}_$file"
    mv "$file" "${PWD##*/}_$file"
  done
)

ren "${@:-.}"


omnio 03-20-2007 05:46 AM

Obviously, cfaj was right in everything he said.

Quote:

Originally Posted by me4linux
I didnt come across dirname and basename till now....

You don't *have to* use them, but here is a quite clear explanation of them: http://www-128.ibm.com/developerwork...sh.html#N100E0


Quote:

Originally Posted by me4linux
What mistake is there in my approach...

No mistake, just some things need to be added.

Here is a slightly less complicated code (actually I like cfaj's style more but maybe this will look less cryptic to you):

Code:

#!/bin/sh
# don't name this script "rename".

do_rename() {
        [ -d "$1" ] || { echo "error: you must specify A DIRECTORY" ; exit 1 ; }
        echo ; echo "$1 is a directory, now descending into it."

        # enter this directory
        cd "$1"
        # preserve this directory name
        local MYDIR="${PWD}"

        # make sure only the last part of a path is prepended;
        # this also helps when an absolute path is passed as argument.
        PREPEND=$(basename "$1")

        for FILE in *
        do
                if [ -d "${FILE}" ]  ; then
                        do_rename "${FILE}"
                        # now exit from this directory
                        cd "${MYDIR}"
                else
                        echo "${FILE}" is renamed to "${PREPEND}_${FILE}"
                        mv "$FILE" "${PREPEND}_${FILE}"
                fi
        done
}
do_rename "$1"

As far as I can tell, both scripts (cfaj's and mine) work with both relative and absolute paths, so you can launch them like this:

Code:

./rename mydir

-- or--

./rename /path/to/mydir

I don't know why you insist on this:
Code:

./rename ./mydir
since it is a really different fish; see this: http://mdobson.home.cern.ch/mdobson/...scripting.html

Sorry for the previous broken code, I was on a rush. Cheers.

EDIT: changed the code to work with absolute paths.


All times are GMT -5. The time now is 05:34 AM.