21
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

我无法对此进行浮点比较。我哪里错了?

4

2 回答 2

29

Bash 本身不能使用浮点数。在这种情况下,也许您可​​以乘以 10 或 100(等)并获得可以比较的整数值。或者,您可以使用bc比较和返回值:

echo "10.2>10.1" | bc
于 2012-07-18T12:44:34.733 回答
14
# 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 '
于 2012-07-18T15:45:35.350 回答