Robert P. J. Day wrote:
On Tue, 11 Feb 2003, Gordon wrote:


I've searched all over for an answer to this and haven't found one yet!

Is there a way in a bash script to test if a variable contains an integer? I want to create a script that uses some simple arithmetic but bash bombs out if you try to do a comparison or arithmetic with a non-numeric value. Basically, something like:

read var
if [ "$var" is a number ]; then
do something
else
echo var is not a number
fi

$ man expr

  the "expr" utility is, IMNSHO, sadly overlooked.

rday



Thanks for the suggestion. I figured out that I can use

$ expr match $var "[0-9]*$"

and it will return >=1 for a valid integer and 0 for an invalid string.

It's just kind of wierd that the shell has builtin math functions, but can't, by itself, tell you if a value is a valid number.



--
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to