comparison operator
hi guys
i have a simple question i want to check integer value in bash scripting i saw a sample code to check this value it was if [ $number -eq $number 2>/dev/null ] then echo "integer" else echo "not integer" fi why this sample code works ? i know we use -eq instead of = to check integer but my question is how it works $number -eq $number ???? |
Hi,
it is because test (invoked using "[") will exit with an error (ie non 0) if a non integer argument is passed to -eq. Evo2. |
-eq is the numerical comparison operator, = is the string comparison operator, so:
Quote:
|
What shell are you using? If it's bash, ksh, or another advanced shell, then you could do something like this instead:
Code:
if [[ ${number//[0-9]} ]]; then Or here's a different way to do it that should work in all bourne-style shells, and is probably more efficient to boot: Code:
case $number in There are many other ways to go about this as well. Code:
[ 5 -eq 05 ] is true, but arithmetic expressions |
All times are GMT -5. The time now is 07:26 AM. |