如何在Bash中进行浮点比较? [英] How can I do float comparison in Bash?

查看:55
本文介绍了如何在Bash中进行浮点比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我无法与此进行浮点比较.我要去哪里错了?

I am not able to perform a float comparison with this. Where am I going wrong?

推荐答案

Bash本身不能使用float.在这种情况下,您可以乘以10或100(以此类推),然后得到可以比较的整数值.或者,您可以使用 bc 比较并返回值:

Bash itself can't use float. In this case maybe you can multiply by 10 or 100 (etc.) and get integer value which you can compare. Or, you can use bc comparison and return value:

echo "10.2>10.1" | bc

这篇关于如何在Bash中进行浮点比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆