This question already has answers here:
Closed 4 years ago.
I'm trying to perform simple math, to check if a variable is greater or equal to "1.5"
but I'm getting [: 2.41: integer expression expected
Code:
reSum=$(expr "scale=1;555/230" | bc)
if [ $reSum -ge "1.5" ]; then
...
fi
How can I do floating-point comparisons in shell script?
if echo 555 230 | awk '{exit $1/$2 >= 1.5 ? 0 : 1}'
then
# ...
fi
Edit:
The shortest solution that works for me:
reSum=$(expr "scale=1;555/230" | bc)
if (( `echo $reSum'>='1.5 | bc` )); then
# anything
fi
As pointed out by shellter, [ $(expr "$reSum > 1.5" | bc) ]
actually does a lexicographic comparison.
So, for example, expr "2.4 > 18 | bc" // =>0
.
However, (( `echo $reSum'>='1.5 | bc` ))
does floating point comparison rather than strings.