bash 如何比较bash/awk中的两个十进制数?

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

How to compare two decimal numbers in bash/awk?

bashshellawk

提问by Paused until further notice.

I am trying to compare two decimal values but I am getting errors. I used

我正在尝试比较两个十进制值,但出现错误。我用了

if [ "$(echo $result1 '>' $result2 | bc -l)" -eq 1 ];then

as suggested by the other Stack Overflow thread.

正如其他 Stack Overflow 线程所建议的那样。

I am getting errors.

我收到错误。

What is the correct way to go about this?

解决这个问题的正确方法是什么?

回答by Paused until further notice.

You can do it using Bash's numeric context:

您可以使用 Bash 的数字上下文来做到这一点:

if (( $(echo "$result1 > $result2" | bc -l) )); then

bcwill output 0 or 1 and the (( ))will interpret them as false or true respectively.

bc将输出 0 或 1,(( ))并将它们分别解释为 false 或 true。

The same thing using AWK:

同样的事情使用 AWK:

if (( $(echo "$result1 $result2" | awk '{print ( > )}') )); then

回答by Steven Penny

if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"
then
  echo z not greater than y
else
  echo z greater than y
fi

回答by Samir

Following up on Dennis's reply:

跟进丹尼斯的回复:

Although his reply is correct for decimal points, bash throws (standard_in) 1: syntax errorwith floating point arithmetic.

尽管他的回答对小数点是正确的,但 bash 抛出(standard_in) 1: syntax errorwith floating point algorithm。

result1=12
result2=1.27554e-05


if (( $(echo "$result1 > $result2" | bc -l) )); then
    echo "r1 > r2"
else
    echo "r1 < r2"
fi

This returns incorrect output with a warning although with an exit code of 0.

尽管退出代码为 0,但这会返回错误的输出并带有警告。

(standard_in) 1: syntax error
r1 < r2

(standard_in) 1: 语法错误
r1 < r2

While there is no clear solution to this (discussion thread 1and thread 2), I used following partial fix by rounding off floating point results using awkfollowed by use of bccommand as in Dennis's reply and this thread

虽然对此没有明确的解决方案(讨论线程 1线程 2),但我使用了以下部分修复方法,通过在丹尼斯的回复和此线程中使用awk后跟使用bc命令来舍入浮点结果

Round off to a desired decimal place: Following will get recursive directory space in TB with rounding off at the second decimal place.

四舍五入到所需的小数位:以下将获得以 TB 为单位的递归目录空间,并在小数点后第二位四舍五入。

result2=$(du -s "/home/foo/videos" | tail -n1 | awk '{=/(1024^3); printf "%.2f", ;}')

You can then use bash arithmetic as above or using [[ ]]enclosure as in following thread.

然后,您可以使用上面的 bash 算法或使用如下线程中的[[ ]]外壳。

if (( $(echo "$result1 > $result2" | bc -l) )); then
    echo "r1 > r2"
else
    echo "r1 < r2"
fi

or using -eqoperator where bcoutput of 1 is trueand 0 is false

或使用-eq运算符,其中bc1 的输出为,0 为

if [[ $(bc <<< "$result1 < $result2") -eq 1 ]]; then
    echo "r1 < r2"
else
    echo "r1 > r2"
fi

回答by Timor Kodal

if [[ `echo "$result1 $result2" | awk '{print ( > )}'` == 1 ]]; then
  echo "$result1 is greater than $result2"
fi

回答by progz

You can also echoan if...elsestatement to bc.

您还可以echoif...else来声明bc

- echo $result1 '>' $result2
+ echo "if (${result1} > ${result2}) 1 else 0"

(
#export IFS=2  # example why quoting is important
result1="2.3" 
result2="1.7" 
if [ "$(echo $result1 '>' $result2 | bc -l)" -eq 1 ]; then echo yes; else echo no;fi
if [ "$(echo "if (${result1} > ${result2}) 1 else 0" | bc -l)" -eq 1 ];then echo yes; else echo no; fi
if echo $result1 $result2 | awk '{exit !(  > )}'; then echo yes; else echo no; fi
)

回答by Bernie Bored

Can't bash force type conversion? For example:

不能 bash 强制类型转换吗?例如:

($result1 + 0) < ($result2 + 0)

回答by Pierre Prot

Why use bc ?

为什么使用 bc ?

for i in $(seq -3 0.5 4) ; do echo $i ; if [[ (( "$i" < 2 )) ]] ; then echo "... is < 2";fi; done

The only problem : the comparison "<" doesn't work with negative numbers : they are taken as their absolute value.

唯一的问题:比较“<”不适用于负数:它们被视为它们的绝对值。