LinuxQuestions.org
Register a domain and help support LQ
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 08-06-2011, 09:25 PM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
script with 'tr' failing


Hi Guys,

Can somebody tell me why the following script is failing? its the first one i've tried and i'm getting errors

Code:
#!/bin/bash
#Author: greenpool
#Date:07/08/2011
#Purpose: Illustrate using tr in a script to convert upper to lower filenames

for i in `ls -A`
do
        newname =`echo $i | tr A-Z a-z`
        mv $i $newname
done

Errors:

Code:
test2@ubuntu:~/Documents/temp2a$ ./tr1.sh
./tr1.sh: line 8: newname: command not found
mv: missing destination file operand after `FILE1'
Try `mv --help' for more information.
./tr1.sh: line 8: newname: command not found
mv: missing destination file operand after `FILE2'
Try `mv --help' for more information.
./tr1.sh: line 8: newname: command not found
mv: missing destination file operand after `FILE3'
Try `mv --help' for more information.
./tr1.sh: line 8: newname: command not found
mv: missing destination file operand after `FILE4'
Try `mv --help' for more information.
./tr1.sh: line 8: newname: command not found
mv: missing destination file operand after `FILE5'
Try `mv --help' for more information.
./tr1.sh: line 8: newname: command not found
mv: missing destination file operand after `tr1.sh'
Try `mv --help' for more information.
 
Old 08-06-2011, 09:29 PM   #2
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
Probably there are file names with spaces. Try quoting the variables in the script:

Code:
for i in `ls -A`
do
        newname=`echo "$i" | tr A-Z a-z`
        mv "$i" "$newname"
done

Last edited by Diantre; 08-06-2011 at 09:34 PM. Reason: Removed a space before equal sign, good eye sycamorex!
 
Old 08-06-2011, 09:32 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I think you need to get rid of the space between newname and =.

Also you can do
Quote:
for i in *

Last edited by sycamorex; 08-06-2011 at 09:36 PM.
 
Old 08-06-2011, 09:43 PM   #4
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks guys...yup turns out i had spaces!
 
Old 08-06-2011, 10:04 PM   #5
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Follow-up question

Hello again,

I'm now trying to to do the reverse but also give it exception handling to print a message when it tries to covert the script name from lower case to upper case.

Code:
#!/bin/bash
#Author: Haani Niyaz
#Date:07/08/2011
#Purpose: Illustrate using tr in a script to convert upper to lower filenames

myscriptname=`basename $0`;

for i in `ls -A`
do
        if [ $i = $myscriptname ]
        then
        echo "you can't edit the filename"
        elsif [ $i != $myscriptname ]   
         newname=`echo $i | tr a-z A-Z`
         mv $i $newname
        fi
done
filenames:
file1 file2 file3 file4 file5 tr1.sh


Output:

you can't edit the filename
./tr1.sh: line 13: elsif: command not found

filenames after script execution:
file1 file2 file3 file4 file5 TR1.SH

it also converts the script name from lower case to upper


Appreciate your help a lot, thanks!
 
Old 08-06-2011, 10:07 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by greenpool View Post
Hello again,

I'm now trying to to do the reverse but also give it exception handling to print a message when it tries to covert the script name from lower case to upper case.

Code:
#!/bin/bash
#Author: Haani Niyaz
#Date:07/08/2011
#Purpose: Illustrate using tr in a script to convert upper to lower filenames

myscriptname=`basename $0`;

for i in `ls -A`
do
        if [ $i = $myscriptname ]
        then
        echo "you can't edit the filename"
        elsif [ $i != $myscriptname ]   
         newname=`echo $i | tr a-z A-Z`
         mv $i $newname
        fi
done
filenames:
file1 file2 file3 file4 file5 tr1.sh


Output:

you can't edit the filename
./tr1.sh: line 13: elsif: command not found

filenames after script execution:
file1 file2 file3 file4 file5 TR1.SH

it also converts the script name from lower case to upper


Appreciate your help a lot, thanks!
First of all, Bash uses 'elif' not 'elsif'
 
1 members found this post helpful.
Old 08-06-2011, 10:28 PM   #7
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sycamorex View Post
First of all, Bash uses 'elif' not 'elsif'
Thanks fixed that. the tutorial i'm following had it like that for Bash O_o

i'now getting the following error:

test2@ubuntu:~/Documents/temp2a$ ./tr1.sh
./tr1.sh: line 16: syntax error near unexpected token `fi'
./tr1.sh: line 16: ` fi'
 
Old 08-06-2011, 10:38 PM   #8
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
It seems you're missing the then keyword in the elif:

Code:
elif [ $i != $myscriptname ]; then
 
1 members found this post helpful.
Old 08-06-2011, 10:48 PM   #9
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks guys. I think the tutorial i'm following is outdated cos most of your suggestions were not present..anyway its working now all good!
 
Old 08-07-2011, 05:30 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Congratulations on trying your hand at scripting, and getting your first one to work. It's an enjoyable challenge.

That said, your source does not seem to be the best. Even in this short script there are a few suggestions and improvements that can be made.

1) Be very careful about quoting your variables at all times, unless you want word-splitting to occur. It's vital that you understand how bash parses text, and how whitespace is handled. So read this first: http://mywiki.wooledge.org/Arguments

2) $(..) is highly recommended over `..`

3) Do not parse ls to get lists of filenames. Use globbing instead.

4) The [[ test keyword is safer and more powerful to use than the old [ command. Do pay attention to differences in the way it handles quotes, though, as there are some differences compared to the old test and the rest of the shell.

5) Many external tools like tr and basename are seldom really required, once you know what bash can do internally. As of version 4, bash has two new built-in parameter substitutions for changing the case of strings.

6) You don't need the elif test here. else alone will do the job.

7) Try to use consistent indentations, and don't forget to comment everything.

So your code can be rewritten like this:
Code:
#!/bin/bash

# get the basename of the script
myscriptname="${0##*/}"

#turn on globbing for dotfiles
shopt -s dotglob nullglob

#loop through the filenames to rename them
#don't allow the script itself to be renamed
for i in * ; do

     if [[ "$i" == "$myscriptname" ]] ; then
          echo "you can't edit the filename"

     else
          newname="${i^^}"   # This is a bash v.4 extension.
          #newname="$( echo "$i" | tr 'a-z' 'A-Z' )" # otherwise use this

          mv "$i" "$newname"

     fi

done

exit 0
Of course, you're just practicing right now, so don't worry about getting everything right the first time.

Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start

I highly recommend the first link, at the very least. Do yourself a favor and read it straight through. It will give you the background you need to better understand how the shell works.
 
2 members found this post helpful.
Old 08-07-2011, 05:44 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
By the way, I just realized that there's an even cleaner way to bypass the scriptname itself. Just add it to the GLOBIGNORE shell variable, and it won't be parsed.

Code:
#!/bin/bash

shopt -s dotglob nullglob
GLOBIGNORE="$GLOBIGNORE:*${0##*/}"

for i in * ; do
     newname="${i^^}"
     mv "$i" "$newname"

done
 
Old 08-14-2011, 03:31 AM   #12
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
Congratulations on trying your hand at scripting, and getting your first one to work. It's an enjoyable challenge.

That said, your source does not seem to be the best. Even in this short script there are a few suggestions and improvements that can be made.

1) Be very careful about quoting your variables at all times, unless you want word-splitting to occur. It's vital that you understand how bash parses text, and how whitespace is handled. So read this first: http://mywiki.wooledge.org/Arguments

2) $(..) is highly recommended over `..`

3) Do not parse ls to get lists of filenames. Use globbing instead.

4) The [[ test keyword is safer and more powerful to use than the old [ command. Do pay attention to differences in the way it handles quotes, though, as there are some differences compared to the old test and the rest of the shell.

5) Many external tools like tr and basename are seldom really required, once you know what bash can do internally. As of version 4, bash has two new built-in parameter substitutions for changing the case of strings.

6) You don't need the elif test here. else alone will do the job.

7) Try to use consistent indentations, and don't forget to comment everything.

So your code can be rewritten like this:
Code:
#!/bin/bash

# get the basename of the script
myscriptname="${0##*/}"

#turn on globbing for dotfiles
shopt -s dotglob nullglob

#loop through the filenames to rename them
#don't allow the script itself to be renamed
for i in * ; do

     if [[ "$i" == "$myscriptname" ]] ; then
          echo "you can't edit the filename"

     else
          newname="${i^^}"   # This is a bash v.4 extension.
          #newname="$( echo "$i" | tr 'a-z' 'A-Z' )" # otherwise use this

          mv "$i" "$newname"

     fi

done

exit 0
Of course, you're just practicing right now, so don't worry about getting everything right the first time.

Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start

I highly recommend the first link, at the very least. Do yourself a favor and read it straight through. It will give you the background you need to better understand how the shell works.

David the H. Thanks for going through the effort compiling the information. much appreciated!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] lighttpd startup script failing tkmsr Programming 3 08-07-2010 04:02 PM
Authentication failing in Perl DBI script resetreset Programming 4 11-19-2008 10:01 AM
mount -o loop command failing in startup script for RH 5 dougkdp76 Linux - General 3 01-12-2008 12:04 PM
mkbootdisk script is failing emalossi Fedora 3 01-14-2006 09:31 AM
Startup script failing at bootup, works when logged on cmowl Linux - Enterprise 1 10-20-2005 11:22 PM


All times are GMT -5. The time now is 01:45 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration