今天就跟大家聊聊有关怎么在linux中使用shell判断字符串是否为空,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
help命令可以查看帮助
help test
Linux 下判断字符串是否为空,有一个大坑!
首先想到的两个参数:
-z :判断 string 是否是空串
-n :判断 string 是否是非空串
正确做法:
#!/bin/sh
STRING=
if [ -z "$STRING" ]; then
echo "STRING is empty"
fi
if [ -n "$STRING" ]; then
echo "STRING is not empty"
fi
root@james-desktop:~# ./zerostring.sh
STRING is empty
-------------------------------------------------------------------------
错误做法:
#!/bin/sh
STRING=
if [ -z $STRING ]; then
echo "STRING is empty"
fi
if [ -n $STRING ]; then
echo "STRING is not empty"
fi
输出错误结果:
root@james-desktop:~# ./zerostring.sh
STRING is empty
STRING is not empty
看完上述内容,你们对怎么在linux中使用shell判断字符串是否为空有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。