help with bash shell script !!
I am learning the shell script but i keep getting error message. Can somone kindly point me where i went wrong? (is bash not supporting elseif?)
--------------------------------------------------- #!/bin/bash #Author: dav #Date: xx-xx-xxxx #Purpose : illustrae using tr in a script to convert upper to lower filenames echo $0; # basename get rid of anything except from file/directory name. myscriptname=`basename $0`; #exit; for i in `ls -A` #DO NOT ENTER ANYTHING HERE!! ELSE SYNTAX ERROR do if [ $i = $myscriptname ] then echo "sorry can't rename myself" elseif [ $i != $myscriptname ] newname=`echo $i | tr A-Z a-z` mv $i $newname fi done ---------------------------------------------------- #./tr1.sh #sorry can't rename myself #./tr1.sh: line 19: elsif: command not found #mv: `tr1.sh' and `tr1.sh' are the same file ps: i tried else if , elseif but none of them remove the error message. Thanks in advance |
Let me know if the modified script below helped :
#!/bin/bash #Author: dav #Date: xx-xx-xxxx #Purpose : illustrae using tr in a script to convert upper to lower filenames echo $0; # basename get rid of anything except from file/directory name. myscriptname=`basename $0`; #exit; for i in `ls -A` #DO NOT ENTER ANYTHING HERE!! ELSE SYNTAX ERROR do if [ $i = $myscriptname ] then echo "sorry can't rename myself" else if [ $i != $myscriptname ] then newname=`echo $i | tr A-Z a-z` mv $i $newname fi fi done Regards, Mufy |
In the context of this script it does'nt make any sense to check if it is
the same name and then check if it's not. Your checking for the same information twice. Zack |
this script is suppose to check filename for a list of generated file name. Anyway, but my question is why i can't use elseif ? Mufy suggested a neted if which is not exactly i look for.
Does latest bash inculdes elseif? how do i tell my bash shell's version ? thx |
The else if in bash is elif.
However in your case a second if is actually redundant just like zackarya posted. Also myscriptname=`basename $0`; will result in myscriptname=`basename $0` and not what you want which myscriptnamne= tr1.sh. This is the reason you saw the mv: `tr1.sh' and `tr1.sh' are the same file error. I assume tr1.sh is your actual script. Here is one solution to produce the desired result myscriptname=$( basename $0 ) BTW it helps having a shell programming book when starting to learn. |
rewrite the code again
Error:give a space b/w else if and followed by then and at the end fi.
I rearranged the code listed below,pls go through it and let me know.i executed script successfully and all the files convert into capital letter or vice verse ( change into tr A-Z a-z) this code valid for all the files must be in small letter then this script convert into capital letter ========== ENJOYS ============= #Date: xx-xx-xxxx #Purpose : illustrae using tr in a script to convert upper to lower filenames echo $0; # basename get rid of anything except from file/directory name. myscriptname=`basename $0`; #exit; for i in `ls -A` #DO NOT ENTER ANYTHING HERE!! ELSE SYNTAX ERROR do if [ $i = $myscriptname ] then echo "sorry can't rename myself" else if [ $i != $myscriptname ] then newname=`echo $i | tr a-z A-Z` mv $i $newname fi fi done ~ Quote:
|
All times are GMT -5. The time now is 05:02 PM. |