Shell Script Menus - Rejecting invalid input (KSH)

Greetings all,

I'm currently writing a shell script menu which is dynamically populated from an array. Have a question to ask about the filtering of invalid input. I'm using KSH.

A brief description of my algorithm is as follows:

1) Read in input from user and store in a variable. (a valid input would be a numerical value)
2) Use an if conditional to determine if this variable if smaller than the total number of elements in an array. If so, print out some value and break out of the infinite while loop (a while true loop). Otherwise, print error statement and let loop iterate again.

The code is as follows:

This code works if I enter numerical values. However, the program always executes the if-then portion of the code when i key in some garbage as input (ie. strings, characters). How can I re-write the code such that alphabetical chars and strings would get thrown to the else portion?

Thanks in advance.

Put the if statement in the case structure. Something like the code shown below.

echo enter the value
read val
case $val in

           [0-9]*) if [[ $val -eq $something ]]
           then
           echo $val
           fi;;

           *) echo "Invalid entry" ;;
esac
while true
do
   read choice
   case $choice in
      *[0-9]*) echo "Invalid. Please retry"; continue ;;
   esac

   if  [ $choice -lt ${#sysNames[*]} ]
   then
       echo "something"
       break
   else
       echo "Invalid. Please retry"
   fi
done