How to compare floats in bash
If you need to compare floats in bash
, I suggest to use a system call to bc
, and then check result. Below is a simple sample:
#/bin/bash
LIMIT=75.5
CPU_LOAD=`ps aux | awk '{s +=$3}; END {print s}'`
HOT=`echo "$CPU_LOAD > $LIMIT" | bc`
if [ "$HOT" -eq "1" ]; then
echo "Oh, $CPU_LOAD%! My pants on fire!"
exit 1;
else
echo "Keep cool dude..., it's only $CPU_LOAD%"
fi
Note: first I thought to use expr
, but seems it’s buggy. Check it yourself:
$ expr 36.25 \> 5
Mine says result is false. Weird…