How to compare two floating point numbers in Bash?

前端 未结 17 2159
無奈伤痛
無奈伤痛 2020-11-22 02:17

I am trying hard to compare two floating point numbers within a bash script. I have to variables, e.g.

let num1=3.17648e-22
let num2=1.5

No

17条回答
  •  难免孤独
    2020-11-22 03:01

    please check the below edited code:-

    #!/bin/bash
    
    export num1=(3.17648*e-22)
    export num2=1.5
    
    st=$((`echo "$num1 < $num2"| bc`))
    if [ $st -eq 1 ]
      then
        echo -e "$num1 < $num2"
      else
        echo -e "$num1 >= $num2"
    fi
    

    this works well.

提交回复
热议问题