Mailinglist Archive: opensuse (2532 mails)
| < Previous | Next > |
Re: [opensuse] BASH - Test for Integer Input.
- From: Anders Johansson <ajh@xxxxxxxxxx>
- Date: Sun, 4 May 2008 22:29:43 +0200
- Message-id: <200805042229.43318.ajh@xxxxxxxxxx>
On Sunday 04 May 2008 22:12:54 David C. Rankin wrote:
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
| < Previous | Next > |