1

Trying to get this comparison to work in bash...

As an example, I want the second "comparison" to be true by setting foo=31.26 If the value in $foo is less then or eq to 33.75 OR greater then or eq to 11.25 then "do something". This doesnt work and none if the options are "true" no matter what. What am I doing wrong?

foo="31.26"
case 1 in
 $(bc <<<"11.25 > $foo >= 348.25"))  "do something";;
 $(bc <<<"33.75 >= $foo >= 11.25"))  "do something";;
esac
Jiess
  • 89
  • 6
  • 1
    This might help: [How can I compare two floating point numbers in Bash?](https://stackoverflow.com/q/8654051/3776858) – Cyrus Dec 17 '22 at 17:24
  • 2
    `bc` doesn't support chained comparisons (like `a > b > c`); split it up into binary comparisons and use `&&` or `||` to connect them. – Gordon Davisson Dec 17 '22 at 17:41

1 Answers1

1
if (( $(echo "11.25 > $foo" | bc -l) || $(echo "$foo >= 348.25" | bc -l) ))
then
    echo false
elif (( $(echo "33.75 >= $foo" | bc -l) || $(echo "$foo >= 11.25" | bc -l) ))
then
    echo true
fi
RaHan
  • 26
  • 4