Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
01-18-2005, 06:03 AM
#1
Member
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74
Rep:
Test for integer in BASH
Hi
How to test entered data is integer or not in Bash Shell program.
Thanks in advance
01-18-2005, 06:27 AM
#2
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
Something like this may get you started...
Code:
#!/bin/bash
a=100
if [[ $a != [0-9]* ]]; then
echo not an integer
else
echo $a
fi
01-18-2005, 06:59 AM
#3
Member
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74
Original Poster
Rep:
if line 3 has single [ ]
----------------------------
1 #!/bin/bash
2 a=100
3 if [ $a != [0-9]* ]; then
4 echo "not an integer"
5 else
6 echo $a
7 fi
output
not an integer
------------------------------------
if line 3 has double [[ ]]
----------------------------
1 #!/bin/bash
2 a=100
3 if [ $a != [0-9]* ]; then
4 echo "not an integer"
5 else
6 echo $a
7 fi
output
100
----------------------------------
why ??
01-18-2005, 07:00 AM
#4
Member
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74
Original Poster
Rep:
sorry
line 3 of second code should be
if [[ $a != [0-9]* ]]; then
01-19-2005, 01:03 AM
#6
Member
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74
Original Poster
Rep:
thanks a lot homey
01-19-2005, 08:55 AM
#7
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
look at typeset -i too.
bash and ksh
02-25-2005, 09:27 AM
#8
Member
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388
Rep:
Hi everyone.
It looks to me like homey's script doesn't quite work right. For example:
Code:
#!/bin/bash
a=100a
if [[ $a != [0-9]* ]]; then
echo not an integer
else
echo $a
fi
will output 100a.
This script got me thinking though, and I the following seems to work.
Code:
#!/bin/bash
a=100
if [ "`echo $a | egrep ^[[:digit:]]+$`" = "" ]; then
echo not an integer
else
echo $a
fi
This outputs 100. If a is set to '100a', then it will output 'not an integer.'
Hope this helps!
02-25-2005, 12:51 PM
#9
Member
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108
Rep:
Let test do the job:
a=100a
test $a -eq 0 2>/dev/null
if [ $? -eq 2 ]; then
echo not an integer
else
echo $a
fi
02-25-2005, 11:49 PM
#10
Member
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752
Rep:
I came up with this way (who else has one?), this way also declares it as an integer at the same time for better efficiency.
Code:
#!/bin/bash
declare -i a=100 2>/dev/null
if [ $? -ne 0 ]
then
echo no
else
echo $a
fi
Last edited by Brain Drop; 02-25-2005 at 11:53 PM .
02-27-2005, 06:09 AM
#11
Member
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153
Rep:
Its better to use typeset than declare. They essentially do the same thing & typeset works with ksh too (declare doesnt).
HTH.
02-27-2005, 05:04 PM
#12
Member
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752
Rep:
Yes, however, typeset doesn't work with tcsh, and declare does. It shouldn't matter in this case anyway, since it starts with !#/bin/bash.
03-07-2005, 05:05 AM
#13
Newbie
Registered: Feb 2002
Distribution: Gentoo
Posts: 2
Rep:
Using expr
You can also use expr to ensure a variable is numeric
a=100
if [ `expr $a + 1 2> /dev/null` ] ; then
echo $a is numeric ;
else
echo $a is not numeric ;
fi ;
11-26-2008, 10:24 AM
#14
Member
Registered: Jun 2006
Posts: 125
Rep:
Quote:
Originally Posted by
niall
You can also use expr to ensure a variable is numeric
a=100
if [ `expr $a + 1 2> /dev/null` ] ; then
echo $a is numeric ;
else
echo $a is not numeric ;
fi ;
Isn't there a direct command to check this?
Last edited by tostay2003; 11-26-2008 at 11:15 AM .
11-26-2008, 06:04 PM
#15
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740
No. Shell is a very basic lang, and all internal arithmetic is integer only. You have to use external cmds like bc to do floating point.
You might also look at bash regexes, if you've got v3
http://www.tldp.org/LDP/abs/html/bas...#REGEXMATCHREF
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 05:33 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News