shell脚本的if判断语句
if条件判断语句
if (表达式) #if ( Variable in Array ) 语句1 else 语句2 fi
1、测试数字大小
#!/bin/sh NUM=100 if (( $NUM > 4 )) ;then echo “this num is $NUM greater 4 !” fi
2、测试目录是否存在,不存在则新建目录
!/bin/sh judge dir exist if [ ! -d /test/wxj ];then mkdir -p /test/wxj else echo “This DIR is exist,Please exit …..” fi
逻辑运算符解析:
-f 判断文件是否存在 eg: if [ -f filename ]
-d 判断目录是否存在 eg: if [ -d dir ]
-eq 等于 应用于:整型比较
-ne 不等于 应用于:整型比较
-lt 小于 应用于:整型比较
-gt 大于 应用于:整型比较
-le 小于或等于 应用于:整型比较
-ge 大于或等于 应用于:整型比较
-a 双方都成立(and) 逻辑表达式 –a 逻辑表达式
-o 单方成立(or) 逻辑表达式 –o 逻辑表达式
-z 空字符串
3、多个条件判断
!/bim/sh scores=80 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
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(1)
自己评论下