shell中字符串为空的判断方法

主要有以下几种方法:

echo “$str”|awk '{print length($0)}'
expr length “$str”
echo “$str”|wc -c
但是第三种得出的值会多1,可能是把结束符也计算在内了

判断字符串为空的方法有三种:
if [ "$str" =  "" ]
if [ x"$str" = x ]
if [ -z "$str" ] (-n 为非空)

注意:都要代双引号,否则有些命令会报错,养成好习惯吧!


转自https://blog.csdn.net/gujing001/article/details/7110268

猜你喜欢

转载自blog.csdn.net/mct123/article/details/80596386
今日推荐