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

shell 字符串判空

 

 

#!/bin/bash

PID="test"

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

shell 字符串判空

 

 

3. 补充

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

sh -x test.sh

shell 字符串判空

 

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

shell 字符串判空

上一篇:从0开始学习shell ---简单shell小程序练手


下一篇:AbpVnext使用分布式IDistributedCache Redis缓存(自定义扩展方法)