Tags

, , , , , ,

#!/bin/bash
echo "Enter an integer; I'll test if it's really a one"
read VARIABLE
while [ -z "$VARIABLE" ]; do # Trap empty strings (but there's really no need!)
   echo "Please enter something instead of just hitting ENTER. Thank you!"
    read VARIABLE
done
#
if [ "$VARIABLE" -eq "$VARIABLE" >& /dev/null ]; then # test if integer
    echo "'$VARIABLE' is indeed an integer"
else
     echo "'$VARIABLE' is not an integer"
 fi
 echo "Good bye!"

Save the above in a file, say testint.sh, make it executable and test in the command line. The if statement does an arithmetic comparison which complains a lot if $VARIABLE is not an integer (that’s why there is a redirection!).

Advertisements