How do I compare float / real numbers in a shell script?
Hi i am trying to comapre numbers for the biggest among double type of numbers. For example: 0.254, 0.255, 0.564, 0.984, 0.556, 0.6566, and 0.5666.
I'm using the following script code:
if [ $max -lt $i ]
But they all give the error message "integer expression expected". Help!
The shell is doing exactly what it should be doing, believe it or not. Shell scripts can't really do proper mathematics, just piddly little simple integer math. So when you give it a number like 0.245 it sees "0" and some gobbledygook that it can't figure out, hence the error message.
There are two ways you can solve this. The first is to actually use some sort of more sophisticated mathematical tool like bc to push out the conditional expression, then test its return value to see if your condition was met or not. That's a bit tricky, particularly since bc can be difficult to use.
Instead, if indeed your input is always 0.something then I suggest that you can exploit that and simply strip off the "0." prefix, then compare them as integers and get exactly the result you seek.
Here's how I'd solve that:
while read value
val="$(echo $value | sed 's/0.//')"
if [ $val -gt $max ]
echo "max value is 0.$max"
That should do what you seek as long as your input is always well-formed. Really, if this is going to be something that gets a lot of use, I'd probably also add some code that checks to ensure that the inputs all start with "0." to be sure it never breaks too.
More Useful Shell Script Programming Articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...✔ Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...✔ Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...✔ Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+