LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sh script variable checking (http://www.linuxquestions.org/questions/linux-newbie-8/sh-script-variable-checking-913835/)

lonesoac0 11-16-2011 09:31 AM

Sh script variable checking
 
Hello all,

I have a Bourne shell script and on occasion I add to my script. I have one variable set to a folder directory and a different variable set to a different directory.

My variables are:
ORIGINAL0=/home/user
ORIGINAL1=/etc/networks/

When I do add my variables I sometimes misspell the variables like: OrIGINAL0 or ORIIGINAL1. This messes with my script and frustrates me. I would like to be able to some how check for these kinds of errors before the script runs. Any help would be appreciated!

Juako 11-16-2011 10:39 AM

I've always taken syntax errors as part of the coding activity... yes it may be frustrating at the beginning but instead of looking for some automated thing that corrects your code for you, I'd advice you to get over the feeling and just improve on your attention while coding. With time you'll commit less and less errors.

Should such a tool exist, you still will face debugging and improve your discipline while coding. I don't really think there are other ways.

chrism01 11-16-2011 06:29 PM

AFAIK, there's no pre-flight syntax checker for shell, and there's no way it could know what you want your vars called...
what is very useful for debugging is adding this as the next line after the shebang line
Code:

set -xv
This shows the 'before & after' versions of each line as it is processed; very informative.
The other tip is to write small sections of code & then test them individually (eg using that cmd), don't try to write an entire script in one go.

lonesoac0 11-17-2011 09:09 AM

Thank you for the replies. I did manage to find the following:http://www.grymoire.com/Unix/Sh.html#uh-31 I think that I will try what it suggests and see what happens.

catkin 11-17-2011 12:27 PM

If you include this early in your script, bash will generate an error if it references an unset variable
Code:

set -o nounset


All times are GMT -5. The time now is 05:46 AM.