检查 bash 变量是否等于 0

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

Check if bash variable equals 0

bashequalszero

提问by Perlnika

I have a bash variable depth and I would like to test if it equals 0. In case yes, I want to stop executing of script. So far I have:

我有一个 bash 变量深度,我想测试它是否等于 0。如果是,我想停止执行脚本。到目前为止,我有:

zero=0;

if [ $depth -eq $zero ]; then
    echo "false";
    exit;
fi

Unfortunately, this leads to:

不幸的是,这导致:

 [: -eq: unary operator expected

(might be a bit inaccurate due to translation)

(由于翻译可能有点不准确)

Please, how can I modify my script to get it working?

请问,我如何修改我的脚本以使其正常工作?

回答by cyon

Looks like your depthvariable is unset. This means that the expression [ $depth -eq $zero ]becomes [ -eq 0 ]after bash substitutes the values of the variables into the expression. The problem here is that the -eqoperator is incorrectly used as an operator with only one argument (the zero), but it requires two arguments. That is why you get the unary operatorerror message.

看起来您的depth变量未设置。这意味着在 bash 将变量的值代入表达式后,表达式[ $depth -eq $zero ][ -eq 0 ]变成了。这里的问题是-eq运算符被错误地用作只有一个参数(零)的运算符,但它需要两个参数。这就是为什么您会收到一元运算符错误消息的原因。

EDIT:As Doktor Jmentioned in his comment to this answer, a safe way to avoid problems with unset variables in checks is to enclose the variables in "". See his comment for the explanation.

编辑:正如Doktor J在对此答案的评论中提到的,避免检查中未设置变量出现问题的一种安全方法是将变量包含在"". 有关解释,请参阅他的评论。

if [ "$depth" -eq "0" ]; then
   echo "false";
   exit;
fi

An unset variable used with the [command appears empty to bash. You can verify this using the below tests which all evaluate to truebecause xyzis either empty or unset:

[命令一起使用的未设置变量 在 bash 中显示为空。您可以通过下面的测试中,所有评估来验证这true是因为xyz为空或取消设置:

  • if [ -z ] ; then echo "true"; else echo "false"; fi
  • xyz=""; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi
  • unset xyz; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi
  • if [ -z ] ; then echo "true"; else echo "false"; fi
  • xyz=""; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi
  • unset xyz; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi

回答by Raviteja Gubba

Double parenthesis (( ... ))is used for arithmetic operations.

双括号(( ... ))用于算术运算。

Double square brackets [[ ... ]]can be used to compare and examine numbers (only integers are supported), with the following operators:

[[ ... ]]双方括号可用于比较和检查数字(仅支持整数),具有以下运算符:

· NUM1 -eq NUM2 returns true if NUM1 and NUM2 are numerically equal.

· NUM1 -ne NUM2 returns true if NUM1 and NUM2 are not numerically equal.

· NUM1 -gt NUM2 returns true if NUM1 is greater than NUM2.

· NUM1 -ge NUM2 returns true if NUM1 is greater than or equal to NUM2.

· NUM1 -lt NUM2 returns true if NUM1 is less than NUM2.

· NUM1 -le NUM2 returns true if NUM1 is less than or equal to NUM2.

For example

例如

if [[ $age > 21 ]] # bad, > is a string comparison operator

if [ $age > 21 ] # bad, > is a redirection operator

if [[ $age -gt 21 ]] # okay, but fails if $age is not numeric

if (( $age > 21 )) # best, $ on age is optional

回答by Jacek Dominiak

Try:

尝试:

zero=0;

if [[ $depth -eq $zero ]]; then
  echo "false";
  exit;
fi

回答by Gurubaran

you can also use this format and use comparison operators like '==' '<='

您也可以使用这种格式并使用比较运算符,例如 '==' '<='

  if (( $total == 0 )); then
      echo "No results for "
      return
  fi

回答by Stephen Niedzielski

Specifically: ((depth)). By example, the following prints 1.

具体:((depth))。例如,以下打印1.

declare -i x=0
((x)) && echo $x

x=1
((x)) && echo $x

回答by Prabhat Kumar Singh

You can try this:

你可以试试这个:

: ${depth?"Error Message"} ## when your depth variable is not even declared or is unset.

NOTE: Here it's just ?after depth.

注意:这里就?depth.

or

或者

: ${depth:?"Error Message"} ## when your depth variable is declared but is null like: "depth=". 

NOTE: Here it's :?after depth.

注意:这是:?depth.

Here if the variable depthis found nullit will print the error message and then exit.

在这里,如果depth找到该变量null,它将打印错误消息然后退出。