LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script with 'tr' failing (https://www.linuxquestions.org/questions/linux-newbie-8/script-with-tr-failing-895981/)

greenpool 08-06-2011 08:25 PM

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 :confused:

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.


Diantre 08-06-2011 08:29 PM

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


sycamorex 08-06-2011 08:32 PM

I think you need to get rid of the space between newname and =.

Also you can do
Quote:

for i in *

greenpool 08-06-2011 08:43 PM

Thanks guys...yup turns out i had spaces!

greenpool 08-06-2011 09:04 PM

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 :confused:


Appreciate your help a lot, thanks!

sycamorex 08-06-2011 09:07 PM

Quote:

Originally Posted by greenpool (Post 4435642)
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 :confused:


Appreciate your help a lot, thanks!

First of all, Bash uses 'elif' not 'elsif'

greenpool 08-06-2011 09:28 PM

Quote:

Originally Posted by sycamorex (Post 4435644)
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'

Diantre 08-06-2011 09:38 PM

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

Code:

elif [ $i != $myscriptname ]; then

greenpool 08-06-2011 09:48 PM

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!

David the H. 08-07-2011 04:30 AM

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. 08-07-2011 04:44 AM

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


greenpool 08-14-2011 02:31 AM

Quote:

Originally Posted by David the H. (Post 4435810)
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!


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