bash: test if var is an integer

bash: test if var is an integer

Post by Patrick Scharrenber » Thu, 05 Jun 2003 22:19:08



Hello!

how can i test, if a variable is an integer?

thanks
..patrick

 
 
 

bash: test if var is an integer

Post by Nithyanandham » Thu, 05 Jun 2003 22:41:36



> how can i test, if a variable is an integer?

Here are few ways to do that:

1.
    if [ $var -ne 0 -o $var -eq 0 ] 2> /dev/null
      then
           echo It is an integer
   else
           echo It is not an integer
   fi

2.
case $var in
    *[^0-9]*)
        echo It is not an integer
        ;;
    *)
        echo It is an integer
        ;;
esac

--
Nithyanand.
Siemens, Bangalore, India.

(Please remove 'AtoZ' from my address when replying by mail)

 
 
 

bash: test if var is an integer

Post by Peter S Tillie » Thu, 05 Jun 2003 22:51:17



> Hello!

> how can i test, if a variable is an integer?

> thanks
> ..patrick

If extglob is set (use shopt builtin to do it):

case $var in
    +([0-9])) echo "An integer"
              ;;
    *)        echo "Not an integer"
              ;;
esac

man bash /"Pattern matching"

--
Peter S Tillier
'This post represents the views of the author and does
not necessarily accurately represent the views of BT.'

 
 
 

bash: test if var is an integer

Post by Ed Morto » Thu, 05 Jun 2003 22:54:41




> > how can i test, if a variable is an integer?

> Here are few ways to do that:

> 1.
>     if [ $var -ne 0 -o $var -eq 0 ] 2> /dev/null
>       then
>            echo It is an integer
>    else
>            echo It is not an integer
>    fi

> 2.
> case $var in
>     *[^0-9]*)
>         echo It is not an integer
>         ;;
>     *)
>         echo It is an integer
>         ;;
> esac

The second version wouldn't work as the "^" should be a "!" and it wouldn't
handle negative numbers.

    Ed.

 
 
 

bash: test if var is an integer

Post by Stephane CHAZELA » Fri, 06 Jun 2003 00:04:06


[...]

Quote:>> 1.
>>     if [ $var -ne 0 -o $var -eq 0 ] 2> /dev/null
>>       then
>>            echo It is an integer
>>    else
>>            echo It is not an integer
>>    fi

>> 2.
>> case $var in
>>     *[^0-9]*)
[...]
> The second version wouldn't work as the "^" should be a "!" and it wouldn't
> handle negative numbers.

Subject mentionned "bash". So, it's OK with bash. It's also OK
with ash, zsh and some Bourne shells. But I agree [!...] is
preferable as more portable.

The first one would say " 1 " or "z -o 1" are integers, and
wouldn't work with zsh/ksh93 which perform arithmetic evaluation
on -eq/-ne arguments and know about floating point numbers.

The second claims the empty string is an integer.

case "$var" in
 *[!0-9+-]*|?*[-+]*|""|-|+) echo is not an integer;;
 *) echo is an integer;;
esac

Note that 0x1F [#2]1110 are not claimed to be integers even if
they are for bash arithmetic evaluation.

--
Stphane

 
 
 

bash: test if var is an integer

Post by rakesh shar » Fri, 06 Jun 2003 09:23:48



> Hello!

> how can i test, if a variable is an integer?

> thanks
> ..patrick

one way could be:

if echo "$var" | egrep -s '^[-+]?[0-9]+$'
then
        echo INT
else
        echo NOT INT
fi

 
 
 

bash: test if var is an integer

Post by rakesh shar » Fri, 06 Jun 2003 09:50:14



> Hello!

> how can i test, if a variable is an integer?

if u don't want to invoke any external command, u can do:

case "$var" in
        ""|+|-)          echo "1 '$var' NO";;
        [!0-9+-]*)       echo "2 '$var' NO";;
        [0-9+-]*[!0-9]*) echo "3 '$var' NO";;
        *)               echo "4 '$var' INT";;
esac

 
 
 

bash: test if var is an integer

Post by Michael Wa » Fri, 06 Jun 2003 10:52:49




>Hello!

>how can i test, if a variable is an integer?

function is_integer {
  [[ $1 = ?([+-])+([0-9]) ]]
Quote:}

--

 
 
 

bash: test if var is an integer

Post by Chris F.A. Johnso » Fri, 06 Jun 2003 11:00:01





>>Hello!

>>how can i test, if a variable is an integer?

> function is_integer {
>   [[ $1 = ?([+-])+([0-9]) ]]
> }

$ function is_integer {
Quote:>  [[ $1 = ?([+-])+([0-9]) ]]

bash: syntax error in conditional expression: unexpected token `('
bash: syntax error near `?(['

   extglob must be turned on first:

shopt -s extglob

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

bash: test if var is an integer

Post by Michael Wa » Fri, 06 Jun 2003 11:16:00





>> Hello!

>> how can i test, if a variable is an integer?

>> thanks
>> ..patrick

>If extglob is set (use shopt builtin to do it):

>case $var in
>    +([0-9])) echo "An integer"
>              ;;
>    *)        echo "Not an integer"
>              ;;
>esac

>man bash /"Pattern matching"

case $var in
    ?([+-])+([0-9])) echo "An integer"
              ;;
    *)        echo "Not an integer"
              ;;
esac

would be more correct.
--

 
 
 

bash: test if var is an integer

Post by Peter S Tillie » Fri, 06 Jun 2003 20:34:04






>>> Hello!

>>> how can i test, if a variable is an integer?

>>> thanks
>>> ..patrick

>> If extglob is set (use shopt builtin to do it):

>> case $var in
>>    +([0-9])) echo "An integer"
>>              ;;
>>    *)        echo "Not an integer"
>>              ;;
>> esac

>> man bash /"Pattern matching"

> case $var in
>     ?([+-])+([0-9])) echo "An integer"
>               ;;
>     *)        echo "Not an integer"
>               ;;
> esac

> would be more correct.

You're right, of course.  I was thinking positive integer, I think,
although I didn't bother about the '+'.  The important bit is ensuring
that extglob is set (IME most bash installers don't do it by default).

--
Peter S Tillier
'This post represents the views of the author and does
not necessarily accurately represent the views of BT.'

 
 
 

bash: test if var is an integer

Post by Kevin Rodger » Sat, 07 Jun 2003 02:17:48





>>how can i test, if a variable is an integer?

> function is_integer {
>   [[ $1 = ?([+-])+([0-9]) ]]
> }

How about checking whether the argument plus zero equals itself?

[[ `expr "$1" + 0 2> /dev/null` -eq "$1" ]] 2> /dev/null

--

 
 
 

bash: test if var is an integer

Post by Patrick Scharrenber » Wed, 11 Jun 2003 17:32:34


Hello again!

thanks to all who helped me!

..patrick