当前位置: 移动技术网 > 网络运营>服务器>Linux > shell判断一个变量是否为空方法总结

shell判断一个变量是否为空方法总结

2019年04月17日  | 移动技术网网络运营  | 我要评论

shell中如何判断一个变量是否为空

shell编程中,对参数的错误检查项中,包含了变量是否赋值(即一个变量是否为空),判断变量为空方法如下:

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

#!/bin/sh
para1=
if [ ! -n "$para1" ]; then
  echo "is null"
else
  echo "not null"
fi

【输出结果】"is null"

2.直接通过变量判断

#!/bin/sh
para1=
if [ ! $para1 ]; then
  echo "is null"
else
  echo "not null"
fi

【输出结果】"is null"

3.使用test判断

#!/bin/sh
dmin=
if test -z "$dmin"
then
  echo "dmin is not set!"
else  
  echo "dmin is set !"
fi

【输出结果】"dmin is not set!"

4.使用""判断

#!/bin/sh 
dmin=
if [ "$dmin" = "" ]
then
  echo "dmin is not set!"
else  
  echo "dmin is set !"
fi

【输出结果】"dmin is not set!"

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网