我必须在 shell 脚本中进行除法,最好的方法是:
result1=`echo "scale=3; ($var1 / $total) * 100"| bc -l`
result2=`echo "scale=3; ($var2 / $total) * 100"| bc -l`
但我想比较 $result1
和 $result2
使用 if test $result1 -lt $result2
或 if [ $result1 -gt $result2 ]
无效:(
知道怎么做吗?
最佳答案
您可以使用 expr(1)
比较 float :
: nr@yorkie 3724 ; expr 3.1 '<' 3.3
1
: nr@yorkie 3725 ; expr 3.1 '<' 3.09
0
你也可以有 bc
进行比较和计算:
if [ "$(echo $result1 '<' $result2 | bc -l)" -eq 1 ];then ... fi
终于ksh93可以做算术求值$(($result1 < $result2))
使用 float ,尽管 bash 不能。
关于linux - 如何在 shell 脚本中比较两个浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2769671/