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:
Quote:
while true
do
if [$choice -lt ${#sysNames[*]} ]
then
echo "something"
break
else
echo "Invalid. Please retry"
fi
done
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.