How to compare two decimal numbers in bash/awk?

前端 未结 7 2153
傲寒
傲寒 2020-12-08 15:09

I am trying to compare two decimal values but I am getting errors. I used

if [ \"$(echo $result1 \'>\' $result2 | bc -l)\" -eq 1 ];then

7条回答
  •  醉梦人生
    2020-12-08 15:57

    You can do it using Bash's numeric context:

    if (( $(echo "$result1 > $result2" | bc -l) )); then
    

    bc will output 0 or 1 and the (( )) will interpret them as false or true respectively.

    The same thing using AWK:

    if (( $(echo "$result1 $result2" | awk '{print ($1 > $2)}') )); then
    

提交回复
热议问题