Operating System - HP-UX
1834231 Members
2352 Online
110066 Solutions
New Discussion

Re: How to validate input as an integer

 
SOLVED
Go to solution
Aji Thomas
Regular Advisor

How to validate input as an integer

hi guys,

I have a script that requires to input hour and minute, But i am unable to validate the input as integer. Is there any unix command that i can include in the shell script to validate it.

Including the shell script
-------------------------------------------

printf "\nSchehule Time"


while [ "$Hour" -lt "1" -o "$Hour" -gt "12" ]
do
printf "\nHour: (1-12) [08] "
read Hour
if [ "$Hour" = "" ]; then
Hour=08;
fi
done
echo "$?"

while [ "$Minute" -lt "1" -o "$Minute" -gt "59" ]
do
printf "Minute: (01-59) [30] "
read Minute
if [ "$Minute" = "" ]; then
Minute=30;
fi
done

printf "\nTime scheduled is $Hour:$Minute\n"

-------------------------------------------
Please advice me,
AJi
7 REPLIES 7
Raj D.
Honored Contributor

Re: How to validate input as an integer

Hi Aji ,

You can use expr to treet that as an integer:

Ex:

i=1
echo $i
while [ $i -le 10 ]
do
echo " $i is less than 10 ]
i=`expr $i + 1`
done
--------------

Hope this will help ,

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Simon Hargrave
Honored Contributor

Re: How to validate input as an integer

You can use typeset to force variables to be integers. So: -

typeset -i hour
read hour

This will then guarantee that $hour is an integer. If the user enters text instead of a number, the value will be 0

You can then validate the range is between 1 and 12 as required, happy that $hour will be a proper integer.
RAC_1
Honored Contributor
Solution

Re: How to validate input as an integer

come case statements.

echo "key in hour"

read hour

case $hour in
[0-9]|1[0-9]|2[0-9])
echo "your choice - $hour"
echo "going ahead";;
*)
echo "bad choice for hour-$hour"
;;
esac
There is no substitute to HARDWORK
Raj D.
Honored Contributor

Re: How to validate input as an integer

Hi Aji ,


Here you have ti change ,

read Minute
if [ "$Minute" = "" ]; then

You need to remove the quote to treet as a integer , and you can do the comparisiion statement like , -le (Less than ) -ge (Greater than ) or -eq (Equal ) , or = ,

so try making :

read Minute
if [ $Minute = integer_value ]
then
..

Also you can use the typset -i ,


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: How to validate input as an integer

Hi RAC,

I hope you provided the info. But please explain how to limit hours betwwen 1-12 with the case syntax

Regards
AJi
Raj D.
Honored Contributor

Re: How to validate input as an integer

Aji ,

its typeset , sory , mistake in my last post. np pls.

" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: How to validate input as an integer

Hi RAC,

I got the logic of the case syntax. Thanks a lot. It helps me to solve the problem

Thanks for the support guys.
regards,
AJi