bash float比较和(standard_in)1:语法错误(bash float comparison and (standard_in) 1: syntax error)

我有这段代码,我得到(standard_in) 1: syntax error来自第三行的(standard_in) 1: syntax error :

valueInFloat=printf "%.2g" $temp tempFloat=printf "%.1f" $value2 compare_result=`echo "$tempFloat < $valueInFloat" |bc -l` if [[ -z $compare_result ]] then ...

我试图在bash进行浮点值比较。

请注意,如果我注释掉第三行并将compare_result=0 ,则错误消失。

I have this piece of code where I am getting (standard_in) 1: syntax error from the third line:

valueInFloat=printf "%.2g" $temp tempFloat=printf "%.1f" $value2 compare_result=`echo "$tempFloat < $valueInFloat" |bc -l` if [[ -z $compare_result ]] then ...

I am trying to do float value comparison in bash.

Note that, if I comment out the third line and put compare_result=0, the error goes away.

最满意答案

在某些情况下, printf可以生成bc无法识别的浮点值。 具体来说,类似于:

pax> printf "%.2g\n" 42456456457357357 4.2e+16 pax> echo '4.2e+16 > 1.0' | bc -l (standard_in) 1: syntax error

我建议你坚持使用%f变种。 它将始终根据ISO C标准生成[−]999.999格式, bc将毫无困难(除非您开始进入无穷大或NaN)。 %g变体生成该格式或%e格式[−]9.999e±99具体取决于请求的值和精度。

另外,你的测试是错误的。 如果字符串为空,则-z测试将为true,并且您的字符串将为1或0具体取决于比较结果。 一个更好的测试是(假设你想在测试结果为肯定时休息:

if [[ ${compare_result} -eq 1 ]]

There are situations in which printf can generate floating point values that bc won't recognise. Specifically, something like:

pax> printf "%.2g\n" 42456456457357357 4.2e+16 pax> echo '4.2e+16 > 1.0' | bc -l (standard_in) 1: syntax error

I suggest you stick with the %f variant. It will always generate the form [−]999.999 as per the ISO C standard, which bc will have no trouble with (unless you start getting into infinities or NaNs). The %g variant generates either that format or the %e format [−]9.999e±99 depending on the value and precision requested.

In addition, your test is wrong. The -z test will be true if the string is empty, and your string will either be 1 or 0 depending on the result of the comparison. A better test would be (assuming you wanted to rest if the test was positive:

if [[ ${compare_result} -eq 1 ]]bash float比较和(standard_in)1:语法错误(bash float comparison and (standard_in) 1: syntax error)

我有这段代码,我得到(standard_in) 1: syntax error来自第三行的(standard_in) 1: syntax error :

valueInFloat=printf "%.2g" $temp tempFloat=printf "%.1f" $value2 compare_result=`echo "$tempFloat < $valueInFloat" |bc -l` if [[ -z $compare_result ]] then ...

我试图在bash进行浮点值比较。

请注意,如果我注释掉第三行并将compare_result=0 ,则错误消失。

I have this piece of code where I am getting (standard_in) 1: syntax error from the third line:

valueInFloat=printf "%.2g" $temp tempFloat=printf "%.1f" $value2 compare_result=`echo "$tempFloat < $valueInFloat" |bc -l` if [[ -z $compare_result ]] then ...

I am trying to do float value comparison in bash.

Note that, if I comment out the third line and put compare_result=0, the error goes away.

最满意答案

在某些情况下, printf可以生成bc无法识别的浮点值。 具体来说,类似于:

pax> printf "%.2g\n" 42456456457357357 4.2e+16 pax> echo '4.2e+16 > 1.0' | bc -l (standard_in) 1: syntax error

我建议你坚持使用%f变种。 它将始终根据ISO C标准生成[−]999.999格式, bc将毫无困难(除非您开始进入无穷大或NaN)。 %g变体生成该格式或%e格式[−]9.999e±99具体取决于请求的值和精度。

另外,你的测试是错误的。 如果字符串为空,则-z测试将为true,并且您的字符串将为1或0具体取决于比较结果。 一个更好的测试是(假设你想在测试结果为肯定时休息:

if [[ ${compare_result} -eq 1 ]]

There are situations in which printf can generate floating point values that bc won't recognise. Specifically, something like:

pax> printf "%.2g\n" 42456456457357357 4.2e+16 pax> echo '4.2e+16 > 1.0' | bc -l (standard_in) 1: syntax error

I suggest you stick with the %f variant. It will always generate the form [−]999.999 as per the ISO C standard, which bc will have no trouble with (unless you start getting into infinities or NaNs). The %g variant generates either that format or the %e format [−]9.999e±99 depending on the value and precision requested.

In addition, your test is wrong. The -z test will be true if the string is empty, and your string will either be 1 or 0 depending on the result of the comparison. A better test would be (assuming you wanted to rest if the test was positive:

if [[ ${compare_result} -eq 1 ]]