bash Linux 脚本中 if 表达式中的 -z 值是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19495367/
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
What means the -z value in an if expression on a Linux script?
提问by AndreaNobili
In this script I found this if
expression:
在这个脚本中,我发现了这个if
表达式:
if [ -z ]; then
echo "Usage: createpkg.sh <rev package>"
exit
else
CURRENT_VERSION=
fi
My problem is that I can't find what exactly means this -z
value.
我的问题是我找不到这个-z
值的确切含义。
From the content of the echo I can deduct that (maybe) $1
variable represents the sotware version. and that (maybe) -z
is a void value. So if I execute the script without passing to it the version of the software that I would packing it print me the correct procedure to execute the script.
从回声的内容中,我可以$1
推断出(也许)变量代表软件版本。这(也许)-z
是一个空值。因此,如果我执行脚本而不将其传递给它,我将打包它的软件版本会打印出执行脚本的正确程序。
But I am not sure about the real meaning of the -z
value.
但我不确定该-z
值的真正含义。
回答by fedorqui 'SO stop harming'
From man test
:
来自man test
:
-z STRING
the length of STRING is zero
So the condition:
所以条件:
if [ -z ]; then
means "if the variable $1 is empty". Where $1
is probably the first parameter of the script: if you execute it like ./script <parameter1> <parameter2>
, then $1=parameter1
, $2=parameter2
and so forth.
表示“如果变量 $1 为空”。哪里$1
可能是脚本的第一个参数:如果你像 那样执行它./script <parameter1> <parameter2>
,那么$1=parameter1
,$2=parameter2
等等。
回答by devnull
help test
tells:
help test
告诉:
String operators:
-z STRING True if string is empty.
In your example, the script would print Usage: createpkg.sh <rev package>
and exit if an argument was not supplied.
在您的示例中,Usage: createpkg.sh <rev package>
如果未提供参数,脚本将打印并退出。