linux中条件分支 逻辑而且 和 或者

 

001、 -a 表示而且

[root@PC1 test]# a=5
[root@PC1 test]# b=10
[root@PC1 test]# if [ $a -gt 0 -a $b -gt 0 ]; then echo "yes"; fi     ## -a表示逻辑而且,只有两边同时满足时才返回真
yes
[root@PC1 test]# if [ $a -gt 8 -a $b -gt 8 ]; then echo "yes"; fi

 

 

002、 && 表示逻辑而且

[root@PC1 test]# a=5
[root@PC1 test]# b=10
[root@PC1 test]# if [ $a -gt 0 ] && [ $b -gt 0 ]; then echo "yes"; fi
yes
[root@PC1 test]# if [ $a -gt 8 ] && [ $b -gt 8 ]; then echo "yes"; fi

 

 

003、-o 表示逻辑或者

[root@PC1 test]# a=5
[root@PC1 test]# b=10
[root@PC1 test]# if [ $a -gt 15 -o $b -gt 15 ]; then echo "yes"; fi
[root@PC1 test]# if [ $a -gt 8 -o $b -gt 8 ]; then echo "yes"; fi       ## -o表示逻辑或者,只要有一个语句满足,则返回真
yes

 

 

004、|| 表示逻辑或者

[root@PC1 test]# a=5
[root@PC1 test]# b=10
[root@PC1 test]# if [ $a -gt 15 ] || [ $b -gt 15 ]; then echo "yes"; fi
[root@PC1 test]# if [ $a -gt 8 ] || [ $b -gt 8 ]; then echo "yes"; fi                  ## || 表示逻辑或者,两边只要有一个条件为真,则返回真
yes

 

posted @ 2022-12-08 23:08  小鲨鱼2018  阅读(655)  评论(0编辑  收藏  举报