How to compare two floating point numbers in Bash?

前端 未结 17 2363
無奈伤痛
無奈伤痛 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 02:57

    This script may help where I'm checking if installed grails version is greater than minimum required. Hope it helps.

    #!/bin/bash                                                                                         
    
    min=1.4                                                                                             
    current=`echo $(grails --version | head -n 2 | awk '{print $NF}' | cut -c 1-3)`                         
    
    if [ 1 -eq `echo "${current} < ${min}" | bc` ]                                                          
    then                                                                                                
        echo "yo, you have older version of grails."                                                   
    else                                                                                                                                                                                                                       
        echo "Hurray, you have the latest version" 
    fi
    

提交回复
热议问题