if 判断文件, 目录属性
[-f file] 判断是否是普通文件, 且存在
[-d file] 判断是否是目录, 且存在
[-e file] 判断文件或目录是否存在
[-r file] 判断文件是否可读
[-w file] 判断文件是否可写
[-x file] 判断文件是否可执行
1. 判断数之间的大小
- #!/bin/bash
- #auto if test
- #by zgh 2019
- NUM1=100
- NUM2=200
- if [ $NUME1> $NUM2 ]
- then
- echo "This $NUM1 greate $NUM2 !"
- else
- echo "This $NUM1 little $NUM2 !"
- fi
2. 判断一个目录是否存在
- #!/bin/bash
- #auto if test1
- #by authors zgh 2019
- DIR=/tmp/20191126
- if [ ! -d $DIR ]
- then
- mkdir -p $DIR
- echo -e "\033[32m This $DIR Create successs! \033[0m"
- else
- echo -e "\033[32m This $DIR is exist,Please exit.\033[0m"
- fi
if [ -z "$a" ] 这个表示当变量 a 的值为空时会怎么样
if [ -n "$a" ] 表示当前变量 a 的值不为空
if grep -q '123' 1.txt;then 表示如果 1.txt 种含有'123'的行时会怎么样
if [ ! -e file ];then 表示文件不存在时会怎么样
if (( $a <1 ));then... 等同于 if [ $a -lt 1 ];then ...
[] 中不能使用 <,>,==,!.... 等符号
3. 判断一个文件是否存在
- #!/bin/bash
- #auto test files
- #by authors zgh 2019
- FILES=/tmp/test.txt
- if [ ! -e $FILES ];then
- echo "ok">> $FILES
- else
- echo -e "\033[32m---------------------------\033[1m"
- cat $FILES
- fi
4.if 多条件判断
- #!/bin/bash
- scores=$1
- if [ -z $scores ];then
- echo "Usage: {$0 60|80.}"
- exit
- fi
- if [[ $scores -gt 85 ]]
- then
- echo "very good!";
- elif [[ $scores -gt 75 ]]
- then
- echo "good!";
- elif [[ $scores -gt 60 ]]
- then
- echo "pass!";
- else
- echo "no pass!";
- fi
来源: http://www.bubuko.com/infodetail-3304844.html