Home > HowTo, Linux > Compare float in bash

Compare float in bash

October 27th, 2010 Leave a comment Go to comments

Compare float in bash

If you have a regular if and one of the integers has a float value like 1.200
you will get an error message.

The best way to do is using the bc command.

Like:

$ echo “7.1 > 7.1” | bc
0

$ echo “3.2 > 3.4” | bc
0

$ echo “7.2 < 7.4” | bc
1

Based upon it, we will modify our script, the if condition will be like this
if [ $(echo “$max < $val”|bc) -eq 1 ]
instead of
if [ $val -gt $max ]

So I do
if [ $(echo “$result < $maxconwarn”|bc) -eq 1 ]; then
etc…

It was kind of hard to find that info in the web and lots of people out there
saying to remove the data before the “.” but that was not the real solution.

Categories: HowTo, Linux Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*