shell判断一个变量是否为空

时间:2021-10-04 20:08:27

判断一个变量是否为空 .

1. 变量通过" "引号引起来

如下所示:,可以得到结果为 IS NULL.

#!/bin/sh

para1=

if [ ! -n "$para1" ]; then

echo "IS NULL"

else

echo "NOT NULL"

fi

2. 直接通过变量判断

如下所示:得到的结果为: IS NULL

#!/bin/sh

para1=

if [ ! $para1 ]; then

echo "IS NULL"

else

echo "NOT NULL"

fi

3. 使用test判断

得到的结果就是: dmin is not set!

#!/bin/sh

dmin=

if  test -z "$dmin"  then

echo "dmin is not set!"

else

echo "dmin is set !"

fi

4. 使用""判断

#!/bin/sh

dmin=

if [ "$dmin" = "" ]; then

echo "dmin is not set!"

else

echo "dmin is set !"

fi