- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting...error handling in if...then statements
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 12:34 PM
12-18-2001 12:34 PM
scripting...error handling in if...then statements
#--------------------------------------------------
#!/usr/bin/ksh
MyFunction () {
echo "MyVar should have a value less than 999999...actual value is ${MyVar}...proceeding to test."
set -x
if [ ${MyVar} - le 999999 ]
then
exit 1
fi
exitValue=$?
set +x
echo "Exit status returned by the if...then...fi statement was ${exitValue}."
}
export MyVar=009999
MyFunction
exit
#--------------------------------------------------
The SDTOUT, when I run the above snippet of code is...
MyVar should have a value less than 999999...actual value is 009999...proceeding to test.
+ [ 009999 - le 999999 ]
MyFunction[4]: -: unknown test operator
+ exitValue=0
Exit status returned by the if...then...fi statement was 0.
The test logic (if...then...fi statement) returns an exit status of 0 (successful); however, clearly, the test failed, since it was not able to successfully evalutate the value MyVar due to the typographical error in line MyFunction[4].
What can be done to take this into account, so that the script's error handling catches problems within the syntax of the if...then...fi statement?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 12:39 PM
12-18-2001 12:39 PM
Re: scripting...error handling in if...then statements
should be
if [ ${var} -le 999999 ]
not
if p ${var} - le 999999 ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 12:39 PM
12-18-2001 12:39 PM
Re: scripting...error handling in if...then statements
should be
if [ ${var} -le 999999 ]
not
if [ ${var} - le 999999 ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 12:42 PM
12-18-2001 12:42 PM
Re: scripting...error handling in if...then statements
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 12:50 PM
12-18-2001 12:50 PM
Re: scripting...error handling in if...then statements
if [ if [ $VAL -ge 0 ]
then
echo 'in the if'
elsif [ $VAL -le 0 ]
echo 'in the if'
else
echo "had an error"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 12:56 PM
12-18-2001 12:56 PM
Re: scripting...error handling in if...then statements
That a bit of a really tricky question. I mean if the parser fails the syntax check how is it even supposed to know that it's dealing with an if statement and you want it to somehow evaluate the statement anyway. The best you can do is run sh -n yourscript.sh to test for syntax error before actually executing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 01:45 PM
12-18-2001 01:45 PM
Re: scripting...error handling in if...then statements
the test does properly recognize an error and returns a false (1) for a return code.
and in this case the if statement equates to:
if false
then
exit 1
fi
which does just what it is suppose to to do, ie it does nothing successfully and returns a return code of 0
Jeromy's script returns this value and is correctly displayed.
for Jeromy's script to work as he'd like he'll have to set up his logic properly.
syntax error = false
value < 999999 = false # not true as he has submitted with his script. So, he'd have to write his if statement like this:
if (( $value >= 999999 )) ;then
# or if [ $value -ge 999999 ] ;then
return 0
else
return 1
fi
of course, now he'll have to determine what is causing the "error", a syntax typo or an incorrect value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2001 02:08 PM
12-18-2001 02:08 PM
Re: scripting...error handling in if...then statements
first, functions should return their state with "return" not with "exit" - then your program can access that return-code...
second, you will have to quote each and every substitution (read: everything starting with a $ sign).
HTH,
Wodisch