I am adding two numbers using the below script, but I don't want non-numeric character entry from user so I need to test and throw an error. How do I test the user entry?
echo add 2 numbers
read sum1
read sum2
let sum3=$sum1+$sum2
echo ans is $sum3
I am adding two numbers using the below script, but I don't want non-numeric character entry from user so I need to test and throw an error. How do I test the user entry?
echo add 2 numbers
read sum1
read sum2
let sum3=$sum1+$sum2
echo ans is $sum3
Use printf
for testing if variable is valid integer:
isInt() {
[[ -n "$1" ]] && printf '%f' "$1" >/dev/null 2>&1 &&
echo "valid integer" || echo "invalid integer";
}