if [ ${returnstatus} -gt 0 -a ${returnstatus} -lt "999999" ] ; then
Main Topics
Browse All TopicsI have a problem in a shell program where I do errorchecking.
I ask a returnvalue from the database, which usually is a number, and this goes to a variable.
Now, I want to know if the returnvalue is a number or a string (the erromessage)
A sample of what I have:
tail -2 $log > $templog
head -1 $templog | read returnstatus
if [ $returnstatus > 0 ]
then
errorfunction $returnstatus
fi
This goes well when $returnstatus is 0 or 20000 but, of course, not when $returnstatus is for example 'Arithemic overflow'
I also tried
if [ "$returnstatus" > "0" ]
but still no succes.
what I want is something like:
if $returnvalue between 0 and 999999
then
returnfunction $returnvalue
else
set returnstatus=123456
returnfunction $returnvalue
fi
Or a check if $returnstatus is a number or not, if that is possible.
So, how do I do this in ksh script?
I work om HP-UX.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, as a workaround this works.
I still get an error executing the script but it does continue with a good errormessage.
Sample from my script:
tail -2 $log > $templog
head -1 $templog | read returnstatus
echo $returnstatus
if [ "$returnstatus" -gt "0" ] && [ "$returnstatus" -lt "999999" ]
then
[some script]
else
returnstatus="160032"
[some script]
fi
output on screen:
Arithmetic overflow occurred.
./script.ksh[343]: Arithmetic overflow occurred.: syntax error
output in log (nevermind the dutch language):
Er is een fout opgetreden bij het uitvoeren van de interface.
Melding:
Msg 160032, Level 16, State 1:
You can see it does continue with the correct errorvalue, so it does go into the 'else' part of the statement.
If I could get rid if the syntax error it would be great, if not I'll just have to work with this. I'll leave this question open til tomorrow to see if someone has a great idea...
thanx so far.
Hello,
I've put an 'expr' in the following script to determine if the variable is a string or an integer. In this case you can perform other actions whenever an invalid string is returned:
tail -2 $log | head -1 | read returnstatus
expr $returnstatus + 0 >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Invalid returncode: $returnstatus"
# [some script]
else
if [ $returnstatus -gt 0 -a $returnstatus -lt 999999 ]
then
echo "Valid returncode: $returnstatus"
# [some script]
else
echo "Other returncode: $returnstatus"
returnstatus="160032"
# [some script]
fi
fi
Good luck!
If you're using the Korn shell, you can define a variable as an integer thus:
typeset -i my_number
and then assign it using:
my_number=`a UNIX command` or my_number=7
If you assign a non-numeric value to the variable, it will assign zero to it instead. Therefore, you can then test for a numeric value like this:
if [ my_number -gt 0 ]
then
echo "It's a number!"
else
echo "It's not a number!"
fi
Note that variables typeset in this way can be used without the need for teh '$' or expr syntax e.g. a=a+1; if [ a -eq 10 ]; then echo "It equals ten."; fi
Shaun.
Adding 0 to a variable is the recognised way of achiving a numeric test. However just for fun, why not delete all digits from the string and test if there is anything left.
eg
if [ -n "`echo "$returnstatus" | tr -d [0-9]`]; then
echo "Invalid returncode: $returnstatus"
else
carry on .....
fi
It all depends on how 'perfectionist' or 'anally retentive' we want to be ;-) - this option will return invalid if the returnstatus contains leading or trailing spaces, as a space is not numeric - some of the other options above will not.
Cheers
JJ
Business Accounts
Answer for Membership
by: liddlerPosted on 2003-11-20 at 04:21:07ID: 9786784
How about?
if [ "$returnstatus" -gt "0" ] && [ "$returnstatus" -lt "999999" ]