shell 脚本编程之逻辑运算符 缺乏、安全感 2022-06-11 06:52 185阅读 0赞 ### 逻辑运算符 ### 和大多数语言一样,shell 的逻辑运算符有与或非三种,不同的是 shell 有两套逻辑运算符。 第一套,测试内作逻辑运算 <table> <thead> <tr> <th>逻辑运算符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>[ ! expression ]</td> <td>逻辑非</td> </tr> <tr> <td>[ expression1 -a expression2 ]</td> <td>逻辑与</td> </tr> <tr> <td>[ expression1 -o expression2 ]</td> <td>逻辑或</td> </tr> </tbody> </table> 第二套,测试外作逻辑运算 <table> <thead> <tr> <th>逻辑运算符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>! [ expression ]</td> <td>逻辑非</td> </tr> <tr> <td>[ expression1 ] && [ expression2 ]</td> <td>逻辑与</td> </tr> <tr> <td>[ expression1 ] || [ expression2 ]</td> <td>逻辑或</td> </tr> </tbody> </table> 逻辑运算是短路运算;支持多个逻辑运算符组合。 #learn_logic_operator.sh #!/bin/bash a=10 b=20 c=0 if [ $b -gt $a -a $b -gt $c ]; then echo b is largest number fi if [ $c -lt $a ] && [ $c -lt $b ]; then echo c is less number fi if [ $c -eq 0 ]; then echo c is zero fi if [ ! $a -eq 0 ]; then echo a is not zero fi if ! [ $b -eq 0 ]; then echo b is not zero fi 执行结果 b is largest number c is less number c is zero a is not zero b is not zero
还没有评论,来说两句吧...