LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 01-18-2005, 06:03 AM   #1
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Rep: Reputation: 15
Post Test for integer in BASH


Hi
How to test entered data is integer or not in Bash Shell program.

Thanks in advance
 
Old 01-18-2005, 06:27 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
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
 
Old 01-18-2005, 06:59 AM   #3
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
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 ??
 
Old 01-18-2005, 07:00 AM   #4
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
sorry

line 3 of second code should be


if [[ $a != [0-9]* ]]; then
 
Old 01-18-2005, 07:57 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
This site has a lot of scripting goodies.

http://www.tldp.org/LDP/abs/html/
 
Old 01-19-2005, 01:03 AM   #6
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
thanks a lot homey
 
Old 01-19-2005, 08:55 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
look at typeset -i too.
bash and ksh
 
Old 02-25-2005, 09:27 AM   #8
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 31
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!
 
Old 02-25-2005, 12:51 PM   #9
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
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
 
Old 02-25-2005, 11:49 PM   #10
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Rep: Reputation: 30
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.
 
Old 02-27-2005, 06:09 AM   #11
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Its better to use typeset than declare. They essentially do the same thing & typeset works with ksh too (declare doesnt).

HTH.
 
Old 02-27-2005, 05:04 PM   #12
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Rep: Reputation: 30
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.
 
Old 03-07-2005, 05:05 AM   #13
niall
Newbie
 
Registered: Feb 2002
Distribution: Gentoo
Posts: 2

Rep: Reputation: 0
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 ;
 
Old 11-26-2008, 10:24 AM   #14
tostay2003
Member
 
Registered: Jun 2006
Posts: 125

Rep: Reputation: 15
Quote:
Originally Posted by niall View Post
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.
 
Old 11-26-2008, 06:04 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script 'test' question linmix Programming 5 11-29-2005 11:49 AM
bash integer handling question mikee78 Programming 11 11-14-2005 04:53 AM
Bash Scripting - Using a function as test. ldp Programming 1 01-04-2005 06:31 AM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM
benchmark test bash script PTBmilo Programming 3 02-14-2003 04:37 PM


All times are GMT -5. The time now is 05:33 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration