shell命令-if语句

判断参数的个数

-ne 不等于
-eq 等于
-gt 大于
-lt 小于
-ge 大于等于
-le 小于等于

if [ "$#" -ne 1 ];then
    echo "no equal"
fi

判断文件存在

-f 文件存在
! -f 文件不存在

if [ -f "file.txt" ];then
    echo "file exist"
fi

if [ ! -f "file.txt" ];then
    echo "file no exist"
fi

判断目录存在

-d 目录存在
! -d 目录不存在

if [ -d "fold" ];then
    echo "fold exist"
fi

if [ ! -d "fold" ];then
    echo "fold no exist"
fi

判断字符串相等

== 字符串相等
!= 字符串不相等

if [ "$1" == "-h" ];then
    echo "show help"
fi

判断字符串长度是否为0

-z 字符串长度为0
-n 字符串长度不为0

if [ -z "" ];then
    echo "string length 0"
fi

if [ -n "nonull" ];then
    echo "string length no 0"
fi
原文地址:https://www.cnblogs.com/smallredness/p/11054882.html