bash shell中的浮点比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2424770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:01:45  来源:igfitidea点击:

Floating point comparison in shell

bashshell

提问by Kiran

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 ifstatement. Here is a small code snippet :

您能否向我建议在 Bash 脚本中进行浮点比较的语法?理想情况下,我希望将其用作if声明的一部分。这是一个小代码片段:

key1="12.3"
result="12.2"

if (( $result <= $key1 ))
then
    # some code here
fi

采纳答案by ghostdog74

bash doesn't do floats, use awk

bash 不做浮动,使用 awk

key1=12.3
result=12.5
var=$(awk 'BEGIN{ print "'$key1'"<"'$result'" }')    
# or var=$(awk -v key=$key1 -v result=$result 'BEGIN{print result<key?1:0}')
# or var=$(awk 'BEGIN{print "'$result'"<"'$key1'"?1:0}')
# or 
if [ "$var" -eq 1 ];then
  echo "do something"
else
  echo "result more than key"
fi

there are other shells that can do floats, like zsh or ksh, you might like to try using them as well

还有其他 shell 可以做浮动,比如 zsh 或 ksh,你也可以尝试使用它们

回答by Chen Levy

bcis your friend:

bc是你的朋友:

key1="12.3"
result="12.2"
if [ $(bc <<< "$result <= $key1") -eq 1 ]
    then
    # some code here
fi

Note the somewhat obscure here string(<<<) notation, as a nice alternative to echo "$result <= $key1" | bc.

请注意这里有点晦涩的字符串( <<<) 表示法,它是echo "$result <= $key1" | bc.

Also, the un-bash-like bcprints 1for trueand 0for false.

此外,未bash类似的bc版画10

回答by Aquarius Power

another simple clear way with bc is this:

使用 bc 的另一个简单明了的方法是:

if ((`bc <<< "10.21>12.22"`)); then echo "true"; else echo "false"; fi

回答by Steve Schnepp

Using the exit()function of awkmakes it almost readable.

使用 的exit()功能awk使其几乎可读。

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 ifalready uses the exit value.

请注意,不需要重用[运算符,因为if已经使用了退出值。

回答by linuxaos

### The funny thing about bash is this:
> AA=10.3
> BB=10.4
### It needs `$` for compare
> [[ $AA > $BB ]] && echo Hello
> [[ $AA < $BB ]] && echo Hello
Hello

Yeah, I know it's cheating but it works. And scientific notation does not work here.

是的,我知道这是作弊,但它有效。科学记数法在这里不起作用。

回答by ungalcrys

yu can use this awk comparison inside a if clause, it will print 1 (true) if the condition is true else 0 (false), and those values will be interpreted as boolean vals by the if

你可以在 if 子句中使用这个 awk 比较,如果条件为真,它将打印 1(真)否则 0(假),并且这些值将被 if 解释为布尔值

if (( $(awk 'BEGIN {print ("'$result'" <= "'$key1'")}') )); then
    echo "true"
else
    echo "false"
fi

回答by Rakib Fiha

I was using bc until now, I found in some distros there was not bc installed, and I did not want to go through sudo apt install bcbut python was there. Using python:

直到现在我一直在使用 bc,我发现在一些发行版中没有安装 bc,我不想通过sudo apt install bc但 python 就在那里。使用蟒蛇:

  if python -c "import sys; sys.exit(0 if float($float_1) > float($float_2) else 1)"; 
    then
    echo "true"
         else
           echo "false"
  fi