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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-13-2008, 02:28 PM
|
#1
|
|
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
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,610
|
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
|
|
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
|
|
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
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,610
|
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
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,610
|
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
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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
|
|
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
|
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,043
Rep: 
|
|
|
|
|
| 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 06:49 PM.
|
|
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
|
|