Re: How to determine integer?



2010-03-13, Decare skribis:

Is there any to determine whether a string
is a integer or not? For example,

read s
if [ ... ]; then
do something
else
do another
fi


I came across this kind of hack:

is_int() {
printf "%d" $1 > /dev/null 2>&1
return $?
}

read s
if is_int "$s"; then
echo "$s is an integer."
else
echo "$s is not an integer."
fi

--
kasmra
:wq
.



Relevant Pages

  • Re: set user=%username%
    ... Your most recent feedback confirms that Windows is ... Type this: echo %MyName% ... > usertest=This is a long string. ...
    (microsoft.public.windows.server.general)
  • Re: [opensuse] Back to the basics (bash)
    ... $ echo ... Globbing only expands to existing files that match the pattern, ... or substitution occurs and you get the literal string. ... They both can, at least on linux, so these commands would work. ...
    (SuSE)
  • Re: [opensuse] Back to the basics (bash)
    ... $ echo ... Globbing only expands to existing files that match the pattern, ... or substitution occurs and you get the literal string. ... They both can, at least on linux, so these commands would work. ...
    (SuSE)
  • Re: its not the school home work
    ... I've learned a bit about strong quotes and think I realized when to ... the shell (such as in the $var, but also for the spaces (that ... echo "foo bar" ... you may say that $string does not appear in /list/ context so ...
    (comp.unix.shell)
  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... If you look up the manual on type casting, you'll find that FALSE is cast ... to an empty string, and TRUE is cast to a non-empty string. ...
    (comp.lang.php)