LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-13-2008, 02:28 PM   #1
rbautch
LQ Newbie
 
Registered: Jun 2006
Posts: 13

Rep: Reputation: 0
Compare Decimal Values


How do I run test with decimal values? For example

x=5.2
if [ $x >= 5.1 ]; then
echo "good"
fi

This of course only works with integers, but what about decimals?
 
Old 04-13-2008, 02:53 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,972
Blog Entries: 5

Rep: Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777
Don't use symbol for numeric comparison. Use the "test" special comparisons (you can see all of them with "man test").

-eq = Equals
-ne = Not equal
-gt = Greater than
-ge = Greater than or equal

So what you need is:

Code:
x=5.2
if [ $x -ge 5.1 ]; then
echo "good"
fi
 
Old 04-13-2008, 03:05 PM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You could do:

Code:
x=5.2
if test $(echo "$x >= 5.1" | bc) == 1
  then
  echo "good"
fi
 
Old 04-13-2008, 03:44 PM   #4
rbautch
LQ Newbie
 
Registered: Jun 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlightner View Post
So what you need is:

Code:
x=5.2
if [ $x -ge 5.1 ]; then
echo "good"
fi
This still gives me "integer expression expected".
 
Old 04-13-2008, 03:47 PM   #5
rbautch
LQ Newbie
 
Registered: Jun 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by H_TeXMeX_H View Post
You could do:

Code:
x=5.2
if test $(echo "$x >= 5.1" | bc) == 1
  then
  echo "good"
fi
Thanks. This works, but the script will be run on machines that will likely not have bc. I thought about moving the decimal around to try to normalize values into integers but that seems a little convoluted. Is there any other way?
 
Old 04-14-2008, 05:26 AM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You could probably do it with awk, don't have time to research it now, so check here in the meantime:
http://www.grymoire.com/Unix/Awk.html
 
Old 04-14-2008, 06:16 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
You can try perl like this
Code:
#!/bin/bash
x=5.2
if [ $(perl -e "($x >= 5.1) ? print 1 : print 0") -eq 1 ]
then
    echo good
fi
In perl this is an alternate statement: if the expression is true, execute the statement after the question mark; if the expression is false, execute the statement after the colon.
 
Old 04-14-2008, 11:14 AM   #8
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
With awk you can do:

Code:
echo | awk '{ if (5.2 <= 5.1) print "good" }'
# or for passing variables
x=5.2
echo "$x 5.1" | awk '{ if ($1 <= $2) print "good" }'
Somehow, I'm thinking if those machines don't have bc, they ain't gonna have perl either. If they don't have awk, then throw those machines out the window.

Last edited by H_TeXMeX_H; 04-14-2008 at 11:19 AM.
 
Old 04-14-2008, 12:53 PM   #9
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,972
Blog Entries: 5

Rep: Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777
Quote:
Originally Posted by rbautch View Post
This still gives me "integer expression expected".
My bad. I happened to be running ksh on Linux and it worked in that.

What I typically use for math operations if they aren't handled by simple syntax or expr is the bc command. In bc if a greater than is true then it results in value 1. Test for that:

Something like:
Code:
x=5.2
if [ `echo 6.0\>5.1 |bc` -eq 1 ]; then echo good; fi
Notice you need the backslash in front of the greater than operator to keep the shell from interpreting it as a redirect.
 
Old 04-21-2008, 06:20 PM   #10
rbautch
LQ Newbie
 
Registered: Jun 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by H_TeXMeX_H View Post
With awk you can do:

Code:
echo | awk '{ if (5.2 <= 5.1) print "good" }'
# or for passing variables
x=5.2
echo "$x 5.1" | awk '{ if ($1 <= $2) print "good" }'
Somehow, I'm thinking if those machines don't have bc, they ain't gonna have perl either. If they don't have awk, then throw those machines out the window.
This was the winner. Thanks.
 
Old 04-22-2008, 04:11 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
you can also try the function testfloat():

http://www.linuxquestions.org/questi...69#post3101569
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk/nawk returning decimal values? moutaye Linux - General 6 03-25-2008 05:16 PM
How do I compare two values in separate files xmrkite Linux - Software 11 09-19-2007 11:36 PM
Math with 2 decimal places newbie_adm Linux - Newbie 1 09-15-2006 12:14 AM
Decimal to binary maldini1010 Programming 6 02-01-2005 04:03 PM
c++ binary to decimal conversion.. miguetoo Programming 13 04-24-2003 09:49 PM


All times are GMT -5. The time now is 08:06 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 Google+: linuxquestions
Open Source Consulting | Domain Registration