Linux 比较 bash 中的整数,期望一元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/408975/
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
Compare integer in bash, unary operator expected
提问by Filip Ekberg
The following code gives
以下代码给出
[: -ge: unary operator expected
[: -ge: 预期的一元运算符
when
什么时候
i=0
if [ $i -ge 2 ]
then
#some code
fi
why?
为什么?
回答by Fernando Miguélez
Your piece of script works just great. Are you sure you are not assigning anything else before the if to "i"?
你的脚本很好用。你确定你没有在 if 之前给“i”赋值吗?
A common mistake is also not to leave a space after and before the square brackets.
一个常见的错误也是没有在方括号前后留一个空格。
回答by starblue
Judging from the error message the value of i was the empty string when you executed it, not 0.
从错误信息来看,执行时 i 的值是空字符串,而不是 0。
回答by vladr
Your problem arises from the fact that $i
has a blank value when your statement fails. Always quoteyour variables when performing comparisons if there is the slightest chance that one of them may be empty, e.g.:
您的问题源于$i
当您的语句失败时具有空白值的事实。 如果其中一个可能为空,则在执行比较时始终引用您的变量,例如:
if [ "$i" -ge 2 ] ; then
...
fi
This is because of how the shell treats variables. Assume the original example,
这是因为 shell 如何处理变量。假设原始示例,
if [ $i -ge 2 ] ; then ...
The first thing that the shell does when executing that particular line of code is substitute the value of $i
, just like your favorite editor's search & replacefunction would. So assume that $i
is empty or, even more illustrative, assume that $i
is a bunch of spaces! The shell will replace $i
as follows:
执行该特定代码行时,shell 所做的第一件事就是替换 的值$i
,就像您最喜欢的编辑器的搜索和替换功能一样。因此,假设它$i
是空的,或者更说明性的是,假设它$i
是一堆空格!外壳将替换$i
如下:
if [ -ge 2 ] ; then ...
Now that variable substitutions are done, the shell proceeds with the comparison and.... fails because it cannot see anything intelligible to the leftof -gt
. However, quoting $i
:
现在,变量替换完成后,与比较外壳收益和....失败,因为它看不到任何东西理解到左的-gt
。然而,引用$i
:
if [ "$i" -ge 2 ] ; then ...
becomes:
变成:
if [ " " -ge 2 ] ; then ...
The shell now sees the double-quotes, and knows that you are actually comparing four blanks to 2 and will skip the if
.
shell 现在会看到双引号,并且知道您实际上是在将四个空格与 2 进行比较,并将跳过if
.
You also have the option of specifying a default value for $i
if $i
is blank, as follows:
您还可以选择为$i
if$i
为空指定默认值,如下所示:
if [ "${i:-0}" -ge 2 ] ; then ...
This will substitute the value 0 instead of $i
is $i
is undefined. I still maintain the quotes because, again, if $i
is a bunch of blanks then it does not count as undefined, it will not be replaced with 0, and you will run into the problem once again.
这将替代值0,而不是$i
被$i
不确定。我仍然保留引号,因为再一次,如果$i
是一堆空格,那么它不算作undefined,它不会被 0 替换,并且您将再次遇到问题。
Please read thiswhen you have the time. The shell is treated like a black box by many, but it operates with very few and very simple rules - once you are aware of what those rules are (one of them being how variables work in the shell, as explained above) the shell will have no more secrets for you.
请在有时间时阅读本文。许多人将 shell 视为黑匣子,但它的操作规则很少且非常简单——一旦您了解这些规则是什么(其中之一是变量在 shell 中的工作方式,如上所述),shell 将没有更多的秘密给你。
回答by Roman Newaza
I need to add my 5 cents. I see everybody use [
or [[
, but it worth to mention that they are not part of if syntax.
我需要加上我的 5 美分。我看到每个人都使用[
or [[
,但值得一提的是,它们不是 if 语法的一部分。
For arithmetic comparisons, use ((...))
instead.
对于算术比较,请((...))
改用。
((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed.
See: ArithmeticExpression
((...)) 是一个算术命令,如果表达式非零,则返回退出状态 0,如果表达式为零,则返回 1。如果需要副作用(赋值),也用作“let”的同义词。
请参阅:算术表达式