LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-18-2005, 01:24 AM   #1
taiwf
Member
 
Registered: Jun 2005
Distribution: debian, ubuntu, redhat,knoppix
Posts: 194

Rep: Reputation: 31
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
 
Old 07-18-2005, 01:59 AM   #2
mufy
Member
 
Registered: Oct 2004
Location: Kuwait
Distribution: Currently - AIX | Previously - RHEL 4 ES, FC 10
Posts: 206
Blog Entries: 4

Rep: Reputation: 30
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
 
Old 07-18-2005, 03:10 AM   #3
zackarya
Member
 
Registered: Jul 2003
Distribution: OpenSuse 10, Debian
Posts: 152

Rep: Reputation: 30
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
 
Old 07-18-2005, 05:33 PM   #4
taiwf
Member
 
Registered: Jun 2005
Distribution: debian, ubuntu, redhat,knoppix
Posts: 194

Original Poster
Rep: Reputation: 31
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
 
Old 07-18-2005, 07:23 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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.
 
Old 06-11-2006, 06:07 PM   #6
nitanjandial
LQ Newbie
 
Registered: Jun 2006
Posts: 1

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Shell script bd1308 Linux - Hardware 9 08-22-2005 05:38 PM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
bash shell script naka0naka Linux - Newbie 7 05-28-2004 03:06 PM
BASH shell script help ewarmour Programming 8 05-24-2002 07:57 AM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration