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 Errors: Code:
test2@ubuntu:~/Documents/temp2a$ ./tr1.sh |
Probably there are file names with spaces. Try quoting the variables in the script:
Code:
for i in `ls -A` |
I think you need to get rid of the space between newname and =.
Also you can do Quote:
|
Thanks guys...yup turns out i had spaces!
|
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 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! |
Quote:
|
Quote:
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' |
It seems you're missing the then keyword in the elif:
Code:
elif [ $i != $myscriptname ]; then |
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!
|
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 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. |
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 |
Quote:
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. |