使用shell脚本比较两个浮点数

【参考链接】

http://stackoverflow.com/questions/35734666/floating-point-number-comparison-in-bash-script


【问题】

 I tried to compare floating point number by -gt but it says that point expecting integer value. That means it can not handle floating point number . Then i tried the following code

我想用-gt比较两个浮点数大小,但屏幕却提示期望整数数值。

这就意味着-gt不能处理浮点数。接着我使用以下代码

chi_square=4
if [ "$chi_square>3.84" | bc ]
then
echo yes
else
echo no
fi

但屏幕却输出以下错误信息:

line 3: [: missing `]'
File ] is unavailable.
no

Here the no is echoed but it should be yes. I think that's because of the error it's showing. can anybody help me.

这里,执行结果本该是yes,此刻却提示no。我认为或许与提示的错误信息有关,谁能帮帮我呢?

【解决办法】

1.使用bc

要想使用bc的话请参考以下用法:

if [[ $(bc -l <<< "$chi_square>3.84") -eq 1 ]]; then
   echo 'yes'
else
   echo 'no'
fi


你可能感兴趣的:(shell)