[opensuse] BASH - Test for Integer Input.
Listmates, I ran across this bash test for integers and it works brilliantly. It was quite a contrast to the myriad of case $x in ... *[0-9+-]* ... approaches I ran across. I don't know exactly how it works, but only that it works -- apparently great. If anyone could shed some light on how it works it would be appreciated. I presume it works based upon the differences in the way string and numerical data is held in memory or in a difference in what [ $AM_I_INT ] actually returns and the way the test is carried out. But that is no more than a guess. If anyone has a need, this is a great solution: AM_I_INT="$1" if [ $AM_I_INT -eq $AM_I_INT 2> /dev/null ]; then echo -e "\n\t$AM_I_INT is an integer\n" else echo -e "\n\t$AM_I_INT is not an integer\n" fi Courtesy of: http://minddownload.blogspot.com/2007/06/test-for-integer-in-bash.html -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 04 May 2008 13:12, David C. Rankin wrote:
Listmates,
I ran across this bash test for integers and it works brilliantly. It was quite a contrast to the myriad of case $x in ... *[0-9+-]* ... approaches I ran across. I don't know exactly how it works, but only that it works -- apparently great.
If anyone could shed some light on how it works it would be appreciated. I presume it works based upon the differences in the way string and numerical data is held in memory or in a difference in what [ $AM_I_INT ] actually returns and the way the test is carried out. But that is no more than a guess. If anyone has a need, this is a great solution: AM_I_INT="$1"
if [ $AM_I_INT -eq $AM_I_INT 2> /dev/null ]; then echo -e "\n\t$AM_I_INT is an integer\n" else echo -e "\n\t$AM_I_INT is not an integer\n" fi
A key distinction in the "test" builtin (most commonly accessed using the square bracket notation) is between string equality testing with "=" and numeric equality testing with "-eq". In order for a numerical equality test to be meaningful, both arguments must be interpretable as numbers. So if you try to test "foo" for numeric equality with "foo", you get an error in the attempt to convert "foo" to a number. When that fails, test deems the comparison to have failed. Sending the standard error to dev null with "2> /dev/null" keeps the user from seeing the failure to convert "foo" to an integer. If you try it without standard error redirection, you'll see what happens: % [ "foo" -eq "foo" ] && echo EQ || echo Not EQ bash: [: foo: integer expression expected Not EQ
Courtesy of: http://minddownload.blogspot.com/2007/06/test-for-integer-in-bash.html
-- David C. Rankin
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 04 May 2008 22:12:54 David C. Rankin wrote:
Listmates,
I ran across this bash test for integers and it works brilliantly. It was quite a contrast to the myriad of case $x in ... *[0-9+-]* ... approaches I ran across. I don't know exactly how it works, but only that it works -- apparently great.
If anyone could shed some light on how it works it would be appreciated. I presume it works based upon the differences in the way string and numerical data is held in memory or in a difference in what [ $AM_I_INT ] actually returns and the way the test is carried out. But that is no more than a guess. If anyone has a need, this is a great solution:
AM_I_INT="$1"
if [ $AM_I_INT -eq $AM_I_INT 2> /dev/null ]; then echo -e "\n\t$AM_I_INT is an integer\n" else echo -e "\n\t$AM_I_INT is not an integer\n" fi
If you read the man page for "test" you see that -eq expects integers. If you remove the 2>/dev/null you see that if it's not an integer, it throws an error message. So it "breaks" if it's not an integer, but at the same time it returns "false", so the if-statement as a whole works Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Sunday 04 May 2008 22:12:54 David C. Rankin wrote:
Listmates,
I ran across this bash test for integers and it works brilliantly. It was quite a contrast to the myriad of case $x in ... *[0-9+-]* ... approaches I ran across. I don't know exactly how it works, but only that it works -- apparently great.
If anyone could shed some light on how it works it would be appreciated. I presume it works based upon the differences in the way string and numerical data is held in memory or in a difference in what [ $AM_I_INT ] actually returns and the way the test is carried out. But that is no more than a guess. If anyone has a need, this is a great solution:
AM_I_INT="$1"
if [ $AM_I_INT -eq $AM_I_INT 2> /dev/null ]; then echo -e "\n\t$AM_I_INT is an integer\n" else echo -e "\n\t$AM_I_INT is not an integer\n" fi
If you read the man page for "test" you see that -eq expects integers. If you remove the 2>/dev/null you see that if it's not an integer, it throws an error message. So it "breaks" if it's not an integer, but at the same time it returns "false", so the if-statement as a whole works
Anders
Randall, Anders, Thanks. For the record, did read the man page and I'm familiar with the difference between == and -eq. What I found ingenious was the use of the -eq to compare A -eg A in order to test for an integer. While -eq does expect an integer, I had no idea that that feature could be used in this way or that it would behave in this manner if it got a bum value. I found it cool. -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 04 May 2008 13:12:54 David C. Rankin wrote:
Listmates, [...] If anyone could shed some light on how it works it would be appreciated. I presume it works based upon the differences in the way string and numerical data is held in memory or in a difference in what [ $AM_I_INT ] actually returns and the way the test is carried out. But that is no more than a guess. If anyone has a need, this is a great solution:
AM_I_INT="$1"
if [ $AM_I_INT -eq $AM_I_INT 2> /dev/null ]; then echo -e "\n\t$AM_I_INT is an integer\n" else echo -e "\n\t$AM_I_INT is not an integer\n" fi
Bash test both arguments for integerness, and issues an error (suppressed by 2>/dev/null) on failure. -- Jim
David C. Rankin wrote:
Listmates,
I ran across this bash test for integers and it works brilliantly. It was quite a contrast to the myriad of case $x in ... *[0-9+-]* ... approaches I ran across. I don't know exactly how it works, but only that it works -- apparently great.
If anyone could shed some light on how it works it would be appreciated. I presume it works based upon the differences in the way string and numerical data is held in memory or in a difference in what [ $AM_I_INT ] actually returns and the way the test is carried out. But that is no more than a guess. If anyone has a need, this is a great solution:
AM_I_INT="$1"
if [ $AM_I_INT -eq $AM_I_INT 2> /dev/null ]; then ^^^^^^^^^^^^ Remove the stderr redirection and all will be revealed ;-)
(It relies on -eq expecting integers.)
echo -e "\n\t$AM_I_INT is an integer\n" else echo -e "\n\t$AM_I_INT is not an integer\n" fi
Courtesy of: http://minddownload.blogspot.com/2007/06/test-for-integer-in-bash.html
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (5)
-
Anders Johansson
-
David C. Rankin
-
Jim Cunning
-
Randall R Schulz
-
Sam Clemens