1

I have a problem with comparing 2 variables (one variable is read from file).

  1 #!/bin/bash
  2
  3 CFR=$(<bubble_status.txt)
  4 treshold=0.01
  5 echo $CFR
  6 echo $treshold
  7
  8 if [ "$CFR" -gt "$treshold" ]
  9 then
 10         echo "CFR is greater then treshold";
 11 elif [ "$CFR" -lt "$treshold" ]
 12 then
 13         echo "CFR is less than treshold";
 14 else
 15         echo "Something else";
 16 fi

But bash returns me this:

19.81
0.01
./dupa7.sh: line 8: [: 19.81: integer expression expected
./dupa7.sh: line 11: [: 19.81: integer expression expected
Something else

Any ideas?

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
Damian Silkowski
  • 127
  • 2
  • 13

2 Answers2

2

Bash doesn't support float comparison. You could use bc:

if (( $(bc <<< "$CFR > $threshold") )); then
    echo "CFR is greater then threshold"
fi
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
-1

Bash doesn't support floating point arithmetic. Use another tool such as bc or awk if you want to do floating point comparison/math.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • comprehensive list of methods in this post http://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-a-bash-script – terence hill Jan 20 '16 at 13:18