Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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
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.
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.
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
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
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.
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.