Hi
If I want to read user input and want to validate if it is a numeric number in some range of 1-100 what would be the best way?
Sabina
Hi
If I want to read user input and want to validate if it is a numeric number in some range of 1-100 what would be the best way?
Sabina
You need to tell us which shell and what system. But for ksh this should work
print -n "enter number - "
read val
if [[ $val -ge 1 && $val -le 100 ]] ; then
hi
I am using bourne shell. I used your script it works as long as I enter a number.
I want it to fail if I enter a string by mistake.
Thanks for your reply.
Sabina
print -n "enter number - "
read val
echo $val | grep "[a-zA-Z]"
if test $? -ge 1
then
if [[ $val -ge 1 && $val -le 100 ]] ; then
print "OK"
else
print "NOT in 1-100 range"
fi
else
print "Not a number"
fi
A slight variation on bhargav's script to ensure that only numeric digits are passed, and symbols as well as letters are rejected....
#!/bin/sh
echo "Enter number"
read val
if echo $val | egrep '^[0-9]+$' >/dev/null 2>&1
then
if [ $val -ge 1 -a $val -le 100 ]; then
echo "OK"
else
echo "Out of range"
fi
else
echo "Not a number"
fi
exit 0
Cheers
ZB
hi Zb and bhargav
Thanks a lot for your time. It was really useful.
Sabina