Shellif的使用详解_与的使用详解

Shellif的使用详解_与的使用详解

2023年8月2日发(作者:)

Shellif的使⽤详解_与的使⽤详解1 IF使⽤详解1 格式1.1 单分⽀语句结构if [ 条件表达式 ]; then 指令

fi1.2 双分⽀语句结构if [ 条件表达式 ]; then

指令⼀

else

指令⼆

fi1.3 多分⽀语句结构if [ -f file ]; then echo "yes yes yes"elif [ -z file ]; then echo "yes yes"else echo "nonono"fi上⾯直接给出了多分⽀if语句的⼀个实例。从上⾯三个结构中可以看出,条件表达式的左右都要有空格。

2 条件表达式的内容2.1 字符串的判断str1 = str2      当两个串有相同内容、长度时为真

str1 != str2      当串str1和str2不等时为真

-n str1        当串的长度⼤于0时为真(串⾮空)

-z str1        当串的长度为0时为真(空串)

str1         当串str1为⾮空时为真2.2 数字的判断int1 -eq int2    两数相等为真

int1 -ne int2    两数不等为真

int1 -gt int2    int1⼤于int2为真

int1 -ge int2    int1⼤于等于int2为真

int1 -lt int2    int1⼩于int2为真

int1 -le int2    int1⼩于等于int2为真2.3 ⽂件的判断-r file     ⽤户可读为真

-w file     ⽤户可写为真

-x file     ⽤户可执⾏为真

-f file     ⽂件为正规⽂件为真

-d file     ⽂件为⽬录为真

-c file     ⽂件为字符特殊⽂件为真

-b file     ⽂件为块特殊⽂件为真

-s file     ⽂件⼤⼩⾮0时为真

-t file     当⽂件描述符(默认为1)指定的设备为终端时为真2.4 复杂逻辑判断条件表达式中可能有多个条件,需要使⽤与或⾮等逻辑操作。-a         与

-o        或

!        ⾮2.5 ⼀个实例if [ $score -ge 0 ]&&[ $score -lt 60 ];then echo "sorry,you are lost!"elif [ $score -ge 60 ]&&[ $score -lt 85 ];then echo "just soso!"elif [ $score -le 100 ]&&[ $score -ge 85 ];then echo "good job!"else echo "input score is wrong , the range is [0-100]!"fi当然,上⾯的实例也可以⽤ -a 来改写:if [ $score -ge 0 -a $score -lt 60 ];then echo "sorry,you are lost!"elif [ $score -ge 60 -a $score -lt 85 ];then echo "just soso!"elif [ $score -le 100 -a $score -ge 85 ];then echo "good job!"else echo "input score is wrong , the range is [0-100]!"fi

3 &&与||的使⽤有时候,我们可以直接使⽤&&和||的组合来代替if表达式。2.1 &&运算符command1 && command2命令之间使⽤ && 连接,实现逻辑与的功能。只有在 && 左边的命令返回真,&& 右边的命令才会被执⾏。只要有⼀个命令返回假(命令返回值 $? == 1),后⾯的命令就不会被执⾏。2.2 ||运算符command1 || command2 命令之间使⽤ || 连接,实现逻辑或的功能。只有在 || 左边的命令返回假,|| 右边的命令才会被执⾏。只要有⼀个命令返回真,后⾯的命令就不会被执⾏。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1690917625a463098.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信