Thursday, January 15, 2009

How to check if a parameter is an integer in bash?

#!/ash/sh
val=$1
if [ ! $(echo "$val" | grep -E "^[0-9]+$") ]; then
echo $val is not a valid integer
else
echo $val is an integer
exit 1
fi


Syntax: ./check.sh 45

2 comments:

Unknown said...

Excellent one!

Glamorous work...keep it up

Chandragupta Borkotoky said...

Hi Syed,

Thanks for the comments. I will try to upload most of my other handy codes as well.