Shell Script to Validate Integer Input
Last Updated :
09 Apr, 2021
Here we are going to see a shell script which validates an integer. We are going to display whether the input entered is an integer or an invalid input.
Approach:
- Take input from the user.
- Store the input of any variable
- Now we are going to trim the input in such a way that all the -(minus) or +(plus) signs before the input are trimmed down.
- Now we are going to apply a regular expression to check if the input pattern has multiple occurrences of digits 0-9.
- If the input pattern contains only digits, this means that the input entered is a valid integer and in all other cases, invalid integer input is shown.
The Shell Script is given below:
# Asking the user to enter an input
echo "Enter an input"
# reading and storing input
read variable
# Applying the approach given above
case ${variable#[-+]} in
*[!0-9]* | '') echo "Not an integer" ;;
* ) echo "Valid integer number" ;;
esac
Output:
Output of the above code snippet
Share your thoughts in the comments
Please Login to comment...