Floating point comparison in shell

前端 未结 7 1418
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 17:38

Can you please suggest to me the syntax for doing floating point comparison in a Bash script? I would ideally like to use it as part of an if statement. Here is

7条回答
  •  时光说笑
    2020-11-27 18:09

    Using the exit() function of awk makes it almost readable.

    key1=12.3
    result=12.5
    
    # the ! awk is because the logic in boolean tests 
    # is the opposite of the one in shell exit code tests
    if ! awk "{ exit ($result <= $key1) }" < /dev/null
    then
            # some code here
    fi
    

    Note that there is not need to reuse the [ operator as if already uses the exit value.

提交回复
热议问题