shell中如何判断一个变量是否为空
为了更高效的工作,还是感觉把每天工作的内容都记录下来,更好。虽然简单,但是以后再遇到的时候,直接拿过来用,来提高效率。
今天帮一个同事写一个检测的脚本,有人问我怎么判断变量为空,记录如下:
1.奇怪的写法。不加双引号" "导致的
#!/bin/sh #filename: example.sh para= if [ ! -n $para ] then echo "IS NULL" else echo "NOT NULL" fi
结果:NOT NULL
2.变量通过" "引号引起来
#!/bin/sh para= if [ ! -n "$para" ]then echo "IS NULL" else echo "NOT NULL" fi
结果是:IS NULL
3.直接通过变量判断
#!/bin/sh para= if [ ! $para ]; then echo "IS NULL" else echo "NOT NULL" fi
结果: IS NULL
4.使用test判断
#!/bin/sh dmin= if test -z "$dmin" then echo "dmin is not set!" else echo "dmin is set !" fi
结果:dmin is not set!
5.使用""判断
#!/bin/sh dmin= if [ "$dmin" = "" ] then echo "dmin is not set!" else echo "dmin is set !" fi
结果:dmin is not set!
相关推荐
firefaith 2020-10-30
libao 2020-09-16
Yyqingmofeige 2020-08-18
xiaoyuerp 2020-08-17
以梦为马不负韶华 2020-08-16
huha 2020-10-16
laisean 2020-11-11
大牛牛 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
以梦为马不负韶华 2020-10-20
JohnYork 2020-10-16
Julyth 2020-10-16
applecarelte 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
wangzhaotongalex 2020-09-22
流年浅滩 2020-10-23
liujianhua 2020-10-22
woaimeinuo 2020-10-21
tufeiax 2020-09-03
laisean 2020-09-01
vvu 2020-09-16