LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-17-2012, 04:23 AM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Rep: Reputation: 1
comparison operator


hi guys
i have a simple question

i want to check integer value in bash scripting

i saw a sample code to check this value it was

if [ $number -eq $number 2>/dev/null ]
then
echo "integer"
else
echo "not integer"
fi

why this sample code works ?

i know we use -eq instead of = to check integer
but my question is how it works $number -eq $number ????
 
Old 07-17-2012, 04:33 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,

it is because test (invoked using "[") will exit with an error (ie non 0) if a non integer argument is passed to -eq.

Evo2.
 
1 members found this post helpful.
Old 07-17-2012, 04:51 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,452

Rep: Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765
-eq is the numerical comparison operator, = is the string comparison operator, so:
Quote:
[ 5 -eq 05 ] is true, but
[ 5 = 05 ] is false
otherwise -eq will drop an error message if you try to use it on non-integers.
 
1 members found this post helpful.
Old 07-17-2012, 11:53 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What shell are you using? If it's bash, ksh, or another advanced shell, then you could do something like this instead:

Code:
if [[ ${number//[0-9]} ]]; then
	echo "not an integer"
else
	echo "an integer"
fi
${number//[0-9]} removes every digit from the string. If there's anything left over then it's a non-integer value and the test returns true.

Or here's a different way to do it that should work in all bourne-style shells, and is probably more efficient to boot:

Code:
case $number in

	*[^0-9]*) echo "not an integer" ;;
	       *) echo "an integer" ;;

esac
In this one it globs the string for non-integer characters.

There are many other ways to go about this as well.


Code:
[ 5 -eq 05 ] is true, but
Numbers that begin with 0 are usually treated as octal values. Using values of 08, or 09 can give you funny results. Interestingly enough, the above pattern works just fine (when using 09) when I use "[", but not when I use "[[".

arithmetic expressions
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] String comparison - [: =: unary operator expected hattori.hanzo Programming 2 11-23-2010 08:55 PM
[SOLVED] Strange behaviour in bash comparison when variable contains arithmetic operator michael.wegemer Programming 3 10-19-2010 06:22 AM
[SOLVED] Simple problem getting a comparison operator to work atavus Programming 6 02-08-2010 03:28 PM
Bash file comparison operator question gizmola Programming 5 12-23-2009 09:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration