在shell脚本中,可以使用以下方法判断字符串是否为空:
- 使用
-z
参数:-z
参数检查字符串是否为空,如果为空则返回true,否则返回false。
str="" if [ -z "$str" ]; then echo "字符串为空" else echo "字符串不为空" fi
- 使用
-n
参数:-n
参数检查字符串是否非空,如果非空则返回true,否则返回false。
str="" if [ -n "$str" ]; then echo "字符串不为空" else echo "字符串为空" fi
以上两种方法都是使用了[]
来进行条件判断,可以根据具体情况选择使用。