Hello!
how can i test, if a variable is an integer?
thanks
..patrick
how can i test, if a variable is an integer?
thanks
..patrick
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)
> how can i test, if a variable is an integer?
> thanks
> ..patrick
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.'
> > 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
Ed.
Subject mentionned "bash". So, it's OK with bash. It's also OKQuote:>> 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.
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
> how can i test, if a variable is an integer?
> thanks
> ..patrick
if echo "$var" | egrep -s '^[-+]?[0-9]+$'
then
echo INT
else
echo NOT INT
fi
> how can i test, if a variable is an integer?
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
>how can i test, if a variable is an integer?
--Quote:}
>>how can i test, if a variable is an integer?
> function is_integer {
> [[ $1 = ?([+-])+([0-9]) ]]
> }
bash: syntax error in conditional expression: unexpected token `('Quote:> [[ $1 = ?([+-])+([0-9]) ]]
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
>> 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"
would be more correct.
--
>>> 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.
--
Peter S Tillier
'This post represents the views of the author and does
not necessarily accurately represent the views of BT.'
> function is_integer {
> [[ $1 = ?([+-])+([0-9]) ]]
> }
[[ `expr "$1" + 0 2> /dev/null` -eq "$1" ]] 2> /dev/null
--
1. Testing Whether an environment variable contains integer data in BASH
In Kornshell, I can test to see if an environment variable contains a
valid integer by doing this:
if [[ "x$myvar" = "x+([0-9])" ]]
How can I do something similar in BASH?
2. Linux Configuration... book and CD by Volkerding... CD problems?
3. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH
4. Filesystem <-> Kernel interface
5. test test test test test test test
6. Solaris 2.2 version of play.c?
7. splitting a var with IFS...
8. help with bandwidth management
9. How to set bash IFS to default?
11. Setting of the IFS variable in BASH to omit splitting at tabs and spaces
12. Linux ext2fs IFS for OS/2 : ext2-os2 0.3 available for testing
13. Bash integer comparisons based on user input