Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm writing a script which takes 1 parameter that needs to be an integer between 0 and 100. So basically, I need to do 2 if cases:
1) Is the paramter an iteger?
2) Is it between 0 and 100?I know how to code #2, but how about #1? Can I check that the input is an integer using a Kornshell script?

Hi:
Here's my favorite is numeric check from Bolsky's "The New Kornshell":
#!/bin/ksh
isnum()
{
case $1 in
( ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
( ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
*) return 1;;
esac
}isnum $1 && echo "numeric" || echo "NOT numeric"

Thanks for the reply Nails. What page/chapter is that from? I looked up our library resources and found the Bolsky book you were referring to (it's currently on load).
Actually, I'm having a bit of another problem too. See the code below:
in_percentage=$1
print "Input percentage : $in_percentage"
read CONFIRM?"Please confirm(Y/N) : "
if [[ $CONFIRM != y && $CONFIRM != Y ]]
then
print "Program aborted "
logmsg "Program aborted "
exit 0
fi
if [[ $in_percentage < 0] || $in_percentage > 100 ]]
then
print "The percentage must be between 0 and 100\n"
logmsg "*** TPA4006: Input parameter out of range"
RC=1
exit $RC
fiWhen I enter 82 as my parameter, I always get the "The percentage must be between 0 and 100" message. But clearly 82 is between 0 and 100, so what am I doing wrong? I know that the paramter is correct, because the Confirmation message aways displays 82.

< and > are lexicographical comparison operators in that context.
Try:
if [[ ( $xxx -lt 0 ) || ( $xxx -gt 100 ) ]]

Hi:
The ksh also allows arithmetic comparisions using parenthesis:
if (($in_percentage < 0)) || (($in_percentage > 100))
then
print "The percentage must be between 0 and 100\n"
fiBTW, the isnum function is in the tutorial section of "The New Kornshell", page 78.
Regards,
Nails

I just looked up the isnum function. Am I looking at the code wrong, or is this function for numbers only? What about integers? I need to validate that the input is a whole number between 0 and 100.
Thanks for all the great help so far.

Yes it is rather beside the point! You can use similar logic though:
[[ $xxx = @(100|@([1-9])@([0-9])|@([0-9])) ]]
is true if $xxx is a number in the range 0-100 if written normally.

How about verifying the number of paramters. I have to make sure that only one paramter is entered. So here's what I wrote:
if [[ $# = 0 ]]
then
print "Missing input paramter. Enter 0 - 100"
logmsg "*** TPA4006: Missing input paramter."
RC=1
exit $RC
fi
if [[ $# > 1 ]]
then
print "Please only enter 1 input parameter"
logmsg "*** TPA4006: Incorrect no. of input paramter"
RC=1
exit $RC
fiThe first part works fine, but there are some bugs in the second if clause. If I enter "12 3" as a parameter, the program detects that there are more than 1 parms. But if I enter "1 2 3" then the program fails to recognize that there are more than 1 parms.

Your logic works okay for me and I cannot duplicate the odd behaviour you describe with the pdksh I'm using. Try printing $#,$1,$2 and $3 in a test script with various random parameters to see what's happening.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |