Ree*_*eem 4 linux shell comparison bc conditional-statements
我不得不在shell脚本中进行划分,最好的方法是:
result1=`echo "scale=3; ($var1 / $total) * 100"| bc -l`
result2=`echo "scale=3; ($var2 / $total) * 100"| bc -l`
Run Code Online (Sandbox Code Playgroud)
但我想比较的价值$result1和$result2
使用if test $result1 -lt $result2或if [ $result1 -gt $result2 ]不工作:(
知道怎么做吗?
您可以使用expr(1)以下方法比较浮点数:
: nr@yorkie 3724 ; expr 3.1 '<' 3.3
1
: nr@yorkie 3725 ; expr 3.1 '<' 3.09
0
Run Code Online (Sandbox Code Playgroud)
您还可以bc进行比较以及计算:
if [ "$(echo $result1 '<' $result2 | bc -l)" -eq 1 ];then ... fi
Run Code Online (Sandbox Code Playgroud)
最后,ksh93可以$(($result1 < $result2))使用浮点数进行算术运算,尽管bash不能.