if
- 几种写法
- if 命令; then 命令; fi
- if 命令; then 命令; else 命令; fi
- if 命令; then 命令; elif 命令; then 命令;else 命令; fi
-z 判断变量长度
#!/bin/bash
table="aaa";
if [ ! -z "$table" ] ;then
echo $table
fi
-d 判断目录是否存在
#!/bin/bash
BACKUP_DIR=/opt/backup;
FILE=/opt/test.log
if [ ! -d "$BACKUP_DIR" ];then
mkdir -p $BACKUP_DIR
chmod -R 777 $BACKUP_DIR
echo "create $BACKUP_DIR success.">>$FILE
else
echo "$BACKUP_DIR is exists.">>$FILE
fi
-f 判断文件是否存在
#!/bin/bash
if [ -f /opt/test.txt ] ;then
echo "test.txt is exists";
fi
-省略if之&&和||
#!/bin/bash
[ -f /opt/test.java ] && rm /opt/test.java;
[ -f /opt/test.java ] || exit 0;
-o 或
if [ 表达式1 –o 表达式2 ]
-a 与
if [ 表达式1 –a 表达式2 ]