Linux shell脚本中整数和浮点数的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9939546/
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-08-06 05:30:34  来源:igfitidea点击:

comparison of integer and floating point numbers in shell script

linuxshellunix

提问by Rajeev

In shell script how can we compare (integer and floating point) ,(flaoting point and floating point),(floating point and integer),(integer and integer) with only one if condition.

在shell脚本中,我们如何只用一个if条件比较(整数和浮点数)、(浮点数和浮点数)、(浮点数和整数)、(整数和整数)。

i have few examples like

我有几个例子

 set X=3.1
  set Y=4.1
  if [ $X < $Y ] then
    echo "wassup"
  endif

But running the above from cron job doesnt seem to work.

但是从 cron 作业运行上面的内容似乎不起作用。

采纳答案by brice

The way to carry out floating point operations in bash is to use bcwhich is available on almost all linux distributions.

在 bash 中执行浮点运算的方法是使用bc,它几乎在所有 linux 发行版上都可用。

# bc will return 0 for false and 1 for true
if [ $(echo "23.3 > 7.3" | bc) -ne 0 ] 
then 
  echo "wassup"
fi

There's a good articleavailable on linux journal about floating point math in bash using bc.

linux 杂志上有一篇关于使用 bc 在 bash 中进行浮点数学的好文章

回答by Balaswamy Vaddeman

below example works on bash shell.

下面的示例适用于 bash shell。

 X=3.1
 Y=4.1
 if [ $X -le $Y ]
 then
    echo "wassup"
 fi

you may want to learn shell script here

你可能想在这里学习shell脚本

回答by Peter.O

EDIT. based on the comments to this answer (thanks to user unknownand glenn Hymanman), it seems that when using bcfor a true/falsetest, the required bashtest is simply:

编辑。根据对此答案的评论(感谢用户 unknownglenn Hymanman),似乎在bc用于真/假测试时,所需的bash测试很简单:

  • (( $(echo "$X < $Y" |bc) ))... see the test results and script below
  • (( $(echo "$X < $Y" |bc) ))...查看下面的测试结果和脚本

wheras, the comparison to -ne 0is needed for the old style bash [ ]test.

然而,-ne 0旧式 bash[ ]测试需要与 进行比较。



bashdoes not natively handle floating point numbers, but you can call a utility such as bc

bash本身不处理浮点数,但您可以调用实用程序,例如 bc

From man bc- An arbitrary precision calculator language

From man bc- 一种任意精度的计算器语言

X=3.1
Y=4.1
# This test has two superfluous components. 
# See EDIT (above) and TESTS below
if (($(echo "scale=9; $X < $Y" |bc)!=0)) ;then
    echo "wassup"
fi


TEST results:

检测结果:

if [  "1"  ]   true
   [  "1"  ]   true
if [  "0"  ]   true
   [  "0"  ]   true

if [   1   ]   true
   [   1   ]   true
if [   0   ]   true
   [   0   ]   true

if (( "1" ))   true
   (( "1" ))   true
if (( "0" ))   false
   (( "0" ))   false

if ((  1  ))   true
   ((  1  ))   true
if ((  0  ))   false
   ((  0  ))   false

echo "1<1"|bc  true
echo "1<0"|bc  true


TEST script:

测试脚本:

printf 'if [  "1"  ]   '; if [ "1" ]; then echo true; else echo false; fi
printf '   [  "1"  ]   ';    [ "1" ]  &&   echo true  ||   echo false
printf 'if [  "0"  ]   '; if [ "0" ]; then echo true; else echo false; fi
printf '   [  "0"  ]   ';    [ "0" ]  &&   echo true  ||   echo false
echo  
printf 'if [   1   ]   '; if [  1  ]; then echo true; else echo false; fi
printf '   [   1   ]   ';    [  1  ]  &&   echo true  ||   echo false
printf 'if [   0   ]   '; if [  0  ]; then echo true; else echo false; fi
printf '   [   0   ]   ';    [  0  ]  &&   echo true  ||   echo false
echo  
printf 'if (( "1" ))   '; if (("1")); then echo true; else echo false; fi
printf '   (( "1" ))   ';    (("1"))  &&   echo true  ||   echo false
printf 'if (( "0" ))   '; if (("0")); then echo true; else echo false; fi
printf '   (( "0" ))   ';    (("0"))  &&   echo true  ||   echo false
echo  
printf 'if ((  1  ))   '; if (( 1 )); then echo true; else echo false; fi
printf '   ((  1  ))   ';    (( 1 ))  &&   echo true  ||   echo false
printf 'if ((  0  ))   '; if (( 0 )); then echo true; else echo false; fi
printf '   ((  0  ))   ';    (( 0 ))  &&   echo true  ||   echo false
echo
printf 'echo "1<1"|bc  '; echo "1<1"|bc >/dev/null  && echo true || echo false 
printf 'echo "1<0"|bc  '; echo "1<0"|bc >/dev/null  && echo true || echo false 

回答by user unknown

Bah itself only handles integers. Use bc:

Bah 本身只处理整数。使用公元前:

echo "$X>$Y" | bc 
0
echo "$X<$Y" | bc 
1

You don't need to worry about scale. It is just for the preocision of output formats:

您无需担心规模。它只是为了输出格式的精确性:

X=3.000001
Y=3.0001
echo "$X>$Y" | bc 
0
echo "$X<$Y" | bc 
1
echo "scale=1;$X<$Y" | bc 
1