shell常用用法
shell字符串判空
test.sh
if [ -z "$1" ]; then
echo "string is empty"
fi
结果
sh test.sh
string is empty
sh test.sh test
输出空
shell字符串相等判断
test.sh
if [ $1 = $2 ]
then
echo equal
else
echo not equal
fi
结果
sh test.sh test1 test2
not equal
sh test.sh test1 test1
equal
小结:
- 如果$1和$2其中一个为空,则会报错,
"$1" = "$2"
,改成这种,为空就不会报错了 - 如果要判断不相等,则将
=
改成!=
持续更新