预期的 Bash 一元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15522018/
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
Bash Unary Operator Expected
提问by ThePGtipsy
Okay, so within my script (this is my first time working with bash) I am being met with 2 unary operator expected errors. The code itself is actually working fine, but it's presenting me with these errors at runtime.
好的,所以在我的脚本中(这是我第一次使用 bash),我遇到了 2 个一元运算符预期错误。代码本身实际上工作正常,但它在运行时向我展示了这些错误。
[: !=: unary operator expected
for the line
对于线
if [ ${netmask[1]} != "" ]; do
so for the first error, it's thrown when ${netmask[1]}is ""(null), I have tried multiple ideas and still can't get it to work without returning that error in the process
所以对于第一个错误,当它抛出${netmask[1]}是""(null),我已经尝试了多种想法,但仍不能得到工作,而在这个过程中返回的错误
Any help would be appreciated!
任何帮助,将不胜感激!
EDIT:Solved by adding quotation marks (grrr)
编辑:通过添加引号解决(grrr)
if [ "${netmask[1]}" != "" ]; do
采纳答案by kamituel
If you want to check for the nullvalue for a variable, use -zoperator:
如果要检查null变量的值,请使用-z运算符:
if [ -z "${netmask[1]}" ]; then
On example:
例如:
VAR=""
if [ -z "$VAR" ]; then
echo This will get printed
fi
Edit:Please note the parentheses around the variable: "$VAR".
编辑:请注意变量周围的括号:"$VAR"。

