Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Compare Decimal Values
 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

 04-13-2008, 02:28 PM #1 rbautch LQ Newbie   Registered: Jun 2006 Posts: 13 Rep: 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?
 04-13-2008, 02:53 PM #2 MensaWater LQ Guru   Registered: May 2005 Location: Atlanta Georgia USA Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO Posts: 6,986 Blog Entries: 14 Rep: 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```
 04-13-2008, 03:05 PM #3 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: You could do: Code: ```x=5.2 if test \$(echo "\$x >= 5.1" | bc) == 1 then echo "good" fi```
04-13-2008, 03:44 PM   #4
rbautch
LQ Newbie

Registered: Jun 2006
Posts: 13

Original Poster
Rep:
Quote:
 Originally Posted by jlightner 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".

04-13-2008, 03:47 PM   #5
rbautch
LQ Newbie

Registered: Jun 2006
Posts: 13

Original Poster
Rep:
Quote:
 Originally Posted by H_TeXMeX_H 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?

 04-14-2008, 05:26 AM #6 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: 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
 04-14-2008, 06:16 AM #7 colucix LQ Guru   Registered: Sep 2003 Location: Bologna Distribution: CentOS 6.5 OpenSuSE 12.3 Posts: 10,509 Rep: 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.
 04-14-2008, 11:14 AM #8 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: 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.
04-14-2008, 12:53 PM   #9
MensaWater
LQ Guru

Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,986
Blog Entries: 14

Rep:
Quote:
 Originally Posted by rbautch 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.

04-21-2008, 06:20 PM   #10
rbautch
LQ Newbie

Registered: Jun 2006
Posts: 13

Original Poster
Rep:
Quote:
 Originally Posted by H_TeXMeX_H 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.

 04-22-2008, 04:11 AM #11 konsolebox Senior Member   Registered: Oct 2005 Distribution: Gentoo, Slackware, LFS Posts: 2,248 Blog Entries: 8 Rep: you can also try the function testfloat(): http://www.linuxquestions.org/questi...69#post3101569

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post moutaye Linux - General 6 03-25-2008 05:16 PM xmrkite Linux - Software 11 09-19-2007 11:36 PM newbie_adm Linux - Newbie 1 09-15-2006 12:14 AM maldini1010 Programming 6 02-01-2005 04:03 PM miguetoo Programming 13 04-24-2003 09:49 PM

LinuxQuestions.org

All times are GMT -5. The time now is 11:00 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -