简体   繁体   English

比较bash中的整数,期望一元运算符

[英]Compare integer in bash, unary operator expected

The following code gives 以下代码给出

[: -ge: unary operator expected [:-ge:期望一元运算符

when 什么时候

i=0

if [ $i -ge 2 ]
then
    #some code
fi

why? 为什么?

Your problem arises from the fact that $i has a blank value when your statement fails. 您的问题是由于语句失败时$i具有空值这一事实引起的。 Always quote your variables when performing comparisons if there is the slightest chance that one of them may be empty, eg: 如果有比较小的可能性, 总是在执行比较时引用变量,例如:

if [ "$i" -ge 2 ] ; then
  ...
fi

This is because of how the shell treats variables. 这是因为外壳如何处理变量。 Assume the original example, 假设原始示例,

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 & replace function would. Shell执行该特定代码行时要做的第一件事就是替换$i的值,就像您最喜欢的编辑器的search&replace函数一样。 So assume that $i is empty or, even more illustrative, assume that $i is a bunch of spaces! 因此,假设$i为空,或者甚至更说明性地,假设$i是一堆空格! The shell will replace $i as follows: Shell将替换$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 left of -gt . 现在已经完成了变量替换,shell继续进行比较,并且...失败了,因为它看不到-gt 左侧可理解的任何内容。 However, quoting $i : 但是,引用$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为空,还可以选择为$i指定默认值,如下所示:

if [ "${i:-0}" -ge 2 ] ; then ...

This will substitute the value 0 instead of $i is $i is undefined. 这将用值0代替$i因为$i是未定义的。 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. 我仍然保留引号,因为同样,如果$i是一堆空白,那么它不会算作undefined ,它不会被替换为0,并且您将再次遇到问题。

Please read this when 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就会没有更多秘密供您选择。

I need to add my 5 cents. 我需要加5美分。 I see everybody use [ or [[ , but it worth to mention that they are not part of if syntax. 我看到每个人都使用[[[ ,但是值得一提的是,它们不是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. 如果需要副作用(赋值),也可以用作“ let”的同义词。

See: ArithmeticExpression 请参阅: ArithmeticExpression

从错误消息判断,执行时i的值为空字符串,而不是0。

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. 一个常见的错误是在方括号前后都没有留出空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM