Computing.Net > Forums > Unix > Help - Validating input params.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help - Validating input params.

Reply to Message Icon

Name: tdis
Date: June 8, 2004 at 02:30:51 Pacific
OS: Win 200
CPU/Ram: P4, 1.5G
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Dlonra
Date: June 8, 2004 at 06:58:40 Pacific
Reply:

what defines an integer?

can you translate this into grep'ese?


0

Response Number 2
Name: nails
Date: June 8, 2004 at 09:52:35 Pacific
Reply:

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"



0

Response Number 3
Name: tdis
Date: June 8, 2004 at 20:58:06 Pacific
Reply:

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
fi

When 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.


0

Response Number 4
Name: Wolfbone
Date: June 9, 2004 at 06:49:43 Pacific
Reply:

< and > are lexicographical comparison operators in that context.

Try:

if [[ ( $xxx -lt 0 ) || ( $xxx -gt 100 ) ]]


0

Response Number 5
Name: nails
Date: June 9, 2004 at 09:52:27 Pacific
Reply:

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"
fi

BTW, the isnum function is in the tutorial section of "The New Kornshell", page 78.

Regards,


Nails


0

Related Posts

See More



Response Number 6
Name: tdis
Date: June 9, 2004 at 18:59:23 Pacific
Reply:

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.


0

Response Number 7
Name: Wolfbone
Date: June 9, 2004 at 21:11:06 Pacific
Reply:

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.


0

Response Number 8
Name: tdis
Date: June 9, 2004 at 23:20:11 Pacific
Reply:

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
fi

The 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.


0

Response Number 9
Name: Wolfbone
Date: June 10, 2004 at 00:12:25 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help - Validating input params.

Bash: validating WINDOWS dirnames www.computing.net/answers/unix/bash-validating-windows-dirnames/5194.html

increase year, month,day in a loop www.computing.net/answers/unix/increase-year-monthday-in-a-loop-/5985.html

log file script www.computing.net/answers/unix/log-file-script/4117.html