开发者

Shell script variable not empty (-z option)

How to make sure a variable is not empty with the -z option ?

errorstatus="notnull"
if [ !-z $errorstatus ]
then
   ech开发者_如何学Pythono "string is not null"
fi

It returns the error :

./test: line 2: [: !-z: unary operator expected


Of course it does. After replacing the variable, it reads [ !-z ], which is not a valid [ command. Use double quotes, or [[.

if [ ! -z "$errorstatus" ]

if [[ ! -z $errorstatus ]]


Why would you use -z? To test if a string is non-empty, you typically use -n:

if test -n "$errorstatus"; then
  echo errorstatus is not empty
fi


I think this is the syntax you are looking for:

if [ -z != $errorstatus ] 
then
commands
else
commands
fi


if [ !-z $errorstatus ] just put a space between ! and -z, like this: if [ ! -z $errorstatus ]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜