Compare float in bash

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

VAMOVE *

*