我有一个bash变量depth,我想测试它是否等于0。如果是,我想停止执行脚本。到目前为止,我有:
zero=0;
if [ $depth -eq $zero ]; then
    echo "false";
    exit;
fi不幸的是,这会导致:
 [: -eq: unary operator expected(由于翻译的原因,可能有点不准确)
请告诉我,我如何修改我的脚本才能让它正常工作?
发布于 2012-10-26 19:57:29
双括号(( ... ))用于算术运算。
双方括号[[ ... ]]可用于比较和检查数字(仅支持整数),运算符如下:
· 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.例如
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发布于 2012-10-26 19:37:18
尝试:
zero=0;
if [[ $depth -eq $zero ]]; then
  echo "false";
  exit;
fi发布于 2016-09-24 07:14:01
您还可以使用此格式并使用比较运算符,如'==‘'<=’
  if (( $total == 0 )); then
      echo "No results for ${1}"
      return
  fihttps://stackoverflow.com/questions/13086109
复制相似问题