Bash:预期的整数表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17958855/
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
Bash: Integer expression expected
提问by rabotalius
I'm trying to perform simple math, to check if a variable is greater or equal to "1.5"
but I'm getting [: 2.41: integer expression expected
我正在尝试执行简单的数学运算,以检查变量是否大于或等于“1.5”,但我得到了 [: 2.41: integer expression expected
Code:
代码:
reSum=$(expr "scale=1;555/230" | bc)
if [ $reSum -ge "1.5" ]; then
...
fi
How can I do floating-point comparisons in shell script?
如何在 shell 脚本中进行浮点比较?
回答by Steven Penny
if echo 555 230 | awk '{exit / >= 1.5 ? 0 : 1}'
then
# ...
fi
回答by lifus
Edit:
编辑:
The shortest solution that works for me:
对我有用的最短解决方案:
reSum=$(expr "scale=1;555/230" | bc)
if (( `echo $reSum'>='1.5 | bc` )); then
# anything
fi
As pointed out by shellter, [ $(expr "$reSum > 1.5" | bc) ]
actually does a lexicographic comparison.
So, for example, expr "2.4 > 18 | bc" // =>0
.
正如shellter所指出的,[ $(expr "$reSum > 1.5" | bc) ]
实际上是进行了字典比较。因此,例如,expr "2.4 > 18 | bc" // =>0
。
However, (( `echo $reSum'>='1.5 | bc` ))
does floating point comparison rather than strings.
但是,进行(( `echo $reSum'>='1.5 | bc` ))
浮点比较而不是字符串。