shell 字符串判空

2021-09-01

1. 字符串判空主要用到两个参数

-z  判断字符串为空否

-n  判断字符串不为空

 

2. 实例

#!/bin/bash

PID=`date`

if [ -z "$PID" ]; then
  echo "PID is empty"
else
  echo "PID is not empty"
fi

 

 

#!/bin/bash

PID="test"

if [ -n "$PID" ]; then
  echo "PID is not empty"
fi

 

 

3. 补充

在执行脚本时,有时会报错,有时想要知道脚本执行的情况,可以使用以下命令执行脚本

sh -x test.sh

 

 可以清晰地看到脚本执行的过程

posted @ 2021-09-01 11:32  元月二一  阅读(1104)  评论(0编辑  收藏  举报