LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with bash shell script !! (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-bash-shell-script-344174/)

taiwf 07-18-2005 01:24 AM

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

mufy 07-18-2005 01:59 AM

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

zackarya 07-18-2005 03:10 AM

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

taiwf 07-18-2005 05:33 PM

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

michaelk 07-18-2005 07:23 PM

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.

nitanjandial 06-11-2006 06:07 PM

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:

Originally Posted by taiwf
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



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