How do I validate decimal numbers?
bash, shell
Solution
Instead of using grep, you can use the bash to check expression:
#!/bin/bash
value=98.23
if [[ "$value" =~ ^[0-9]+(\.[0-9]+)?$ ]]
then
echo good
else
echo bad
fi
Problem
Hi I'm working on an assignment and got stuck on this part, how do I validate decimal numbers/numbers in shell? It can accept numbers but not decimal numbers. I want it to be able to accept both. This is what I have so far ``` if echo $value | egrep '^[0-9]+$' >/dev/null 2>&1 ; then echo "OK" else echo "There Is An Error" echo "Please Try Again" fi ```