LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-16-2011, 08:31 AM   #1
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 88

Rep: Reputation: 4
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!
 
Old 11-16-2011, 09:39 AM   #2
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
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.
 
Old 11-16-2011, 05:29 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

Rep: Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026
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.

Last edited by chrism01; 11-17-2011 at 05:21 PM.
 
Old 11-17-2011, 08:09 AM   #4
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 88

Original Poster
Rep: Reputation: 4
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.
 
Old 11-17-2011, 11:27 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
If you include this early in your script, bash will generate an error if it references an unset variable
Code:
set -o nounset
 
1 members found this post helpful.
  


Reply

Tags
bourne, script, scripting


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] Shell Script is showing error after whenever checking a special variable emcykm Linux - Newbie 5 06-01-2011 09:59 AM
[SOLVED] Checking variable against multiple values ksushants Linux - Newbie 6 03-02-2011 09:27 AM
[SOLVED] Checking the existence of env variable mahmoodn Linux - General 8 06-12-2010 09:51 AM
Checking variable is numeric ijf99 Programming 2 03-28-2009 08:13 AM
Checking whether a variable is alphabetic basildon Linux - Newbie 4 09-21-2008 04:03 PM


All times are GMT -5. The time now is 02:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration