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 |
Code:
if [`file $FILE | grep directory`] then Code:
if [`file $FILE | grep directory`] ; then |
Code:
if [ -d $FILE ] ; then |
Code:
#!/bin/sh Code:
./rename test |
[QUOTE=omnio]
Code:
else 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 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 |
Quote:
You mean prepended to the filename. Quote:
Quote:
Quote:
This will produce an endless loop. If you call rename with the name of a directory, it will call itself again and again ... Quote:
If $FILE contains path information, you will be asking to move the file into a probably non-existent directory. Quote:
|
Obviously, cfaj was right in everything he said.
Quote:
Quote:
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 Code:
./rename mydir Code:
./rename ./mydir 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. |