我有一个bash变量depth,我想测试它是否等于0。如果是的话,我希望停止执行脚本。目前我有:
zero=0;
if [ $depth -eq $zero ]; then
echo "false";
exit;
fi
不幸的是,这将导致:
[: -eq: unary operator expected
请问,我应该如何修改我的脚本让它能够正常工作?(由于翻译可能有些不准确,请见谅)
看起来你的 depth
变量未设置。这意味着当Bash将变量的值代入表达式中时,表达式[ $depth -eq $zero ]
变成了 [ -eq 0 ]
。问题在于,-eq
运算符错误地被用作只有一个参数(即零)的运算符,但它需要两个参数。这就是为什么会出现“一元运算符”错误消息的原因。
编辑:正如Doktor J在他对此答案的评论中提到的那样,避免检查时未设置变量的问题的一种安全方法是将变量括在""
中。有关解释,请参见他的评论。
if [ "$depth" -eq "0" ]; then
echo "false";
exit;
fi
在使用 [
命令时,未设置的变量在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
双括号(( ... ))
用于算术运算。
双方括号[[ ... ]]
可用于比较和检查数字(仅支持整数),以下是可用的运算符:
· 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
best, $ on age is optional
It's not working without $
- Jean-Marc Amonif (( age > 21 ))
和 if (( $age > 21 ))
有时是不同的。您可以查看 https://github.com/koalaman/shellcheck/wiki/SC2004 - Jess Chen试试:
zero=0;
if [[ $depth -eq $zero ]]; then
echo "false";
exit;
fi
你也可以使用这种格式,并使用比较运算符如'=='和'<='。
if (( $total == 0 )); then
echo "No results for ${1}"
return
fi
((depth))
。例如,以下代码将打印1
。declare -i x=0
((x)) && echo $x
x=1
((x)) && echo $x
: ${depth?"Error Message"} ## when your depth variable is not even declared or is unset.
注意:在depth
后面只需加上?
。
或者
: ${depth:?"Error Message"} ## when your depth variable is declared but is null like: "depth=".
注意:这里在depth
之后是: ?
。
如果变量depth
的值为null
,它将打印错误消息并退出。
if [ "$depth" -eq "0" ]
;这样,一个未设置的变量($depth
)会被评估为 ""(当然不等于零)。 - Doktor J