Search code examples
bashcomparisonprecision

Bash, compare values


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

Solution

  • 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