Operating System - HP-UX
1828208 Members
2069 Online
109975 Solutions
New Discussion

scripting...error handling in if...then statements

 
Jeromy Gregg
Advisor

scripting...error handling in if...then statements

I have a scripting question about how to handle errors in an if..then...fi statement in a function which I have written to evaluate the value of a variable. My script reads...

#--------------------------------------------------

#!/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?
7 REPLIES 7
Jeff Machols
Esteemed Contributor

Re: scripting...error handling in if...then statements

it looks like you have an extra space in the -le

should be

if [ ${var} -le 999999 ]

not

if p ${var} - le 999999 ]

Jeff Machols
Esteemed Contributor

Re: scripting...error handling in if...then statements

it looks like you have an extra space in the -le

should be

if [ ${var} -le 999999 ]

not

if [ ${var} - le 999999 ]

Jeromy Gregg
Advisor

Re: scripting...error handling in if...then statements

I am aware that there was a type (an extra space between - and le);...that is not my issue. My question is why does if...then return a successful when clearly it failed (due to the typo) and what kind of error handling can be scripted to catch such errors?
Jeff Machols
Esteemed Contributor

Re: scripting...error handling in if...then statements

sorry, I misread your question. I don't if return a status, what you could do is something like this

if [ if [ $VAL -ge 0 ]
then
echo 'in the if'
elsif [ $VAL -le 0 ]
echo 'in the if'
else
echo "had an error"
fi
A. Clay Stephenson
Acclaimed Contributor

Re: scripting...error handling in if...then statements

Hi Jeromy:

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.
If it ain't broke, I can fix that.
Curtis Larson_1
Valued Contributor

Re: scripting...error handling in if...then statements

not quite clay

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.
Wodisch
Honored Contributor

Re: scripting...error handling in if...then statements

Hello,

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