lock_percent=$(echo "scale = 5; $value2*100/$value1" | bc)
value=`echo "$lock_percent" | bc`
if [[ "$value" > "$8" ]]; then
echo "Lock Percentage:$value percentage State Critical"
exit $STATE_CRITICAL
fi
我无法对此进行浮点比较。我哪里错了?
lock_percent=$(echo "scale = 5; $value2*100/$value1" | bc)
value=`echo "$lock_percent" | bc`
if [[ "$value" > "$8" ]]; then
echo "Lock Percentage:$value percentage State Critical"
exit $STATE_CRITICAL
fi
我无法对此进行浮点比较。我哪里错了?
Bash 本身不能使用浮点数。在这种情况下,也许您可以乘以 10 或 100(等)并获得可以比较的整数值。或者,您可以使用bc比较和返回值:
echo "10.2>10.1" | bc
# float number comparison
fcomp() {
awk -v n1="$1" -v n2="$2" 'BEGIN {if (n1+0<n2+0) exit 0; exit 1}'
}
# test and example
fcomp_test() {
if fcomp "$1" "$2"; then
echo "$1<$2"
else
echo "$1>=$2"
fi
}
fcomp_test 0.0 0.1
fcomp_test 0.1 0.1
fcomp_test -0.1 0.1
fcomp_test 1e3 1e4
fcomp_test -1.34e3 1.03e4
fcomp_test ' 0 ' ' 1 '