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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM