上例子:
[centos@kingdom Desktop]$ [[ 7 > 5 && 6 > 5 ]] && echo "Hey"
Hey
[centos@kingdom Desktop]$ echo $?
0
[centos@kingdom Desktop]$ [[ 7 > 8 && 6 > 5 ]] && echo "Hey"
[centos@kingdom Desktop]$ echo $?
0
[centos@kingdom Desktop]$ [[ 7 > 8 || 6 > 5 ]] && echo "Hey"
Hey
[centos@kingdom Desktop]$ echo $?
0
[centos@kingdom Desktop]$ [[ 7 > 5 || 6 > 5 ]] && echo "Hey"
Hey
[centos@kingdom Desktop]$ echo $?
0
只有表达式为真时echo "Hey"才会被执行
[centos@kingdom Desktop]$ [[ 7 > 5 && 6 > 5 ]] || echo "Hey"
[centos@kingdom Desktop]$ echo $?
0
[centos@kingdom Desktop]$ [[ 7 > 8 && 6 > 5 ]] || echo "Hey"
Hey
[centos@kingdom Desktop]$ echo $?
0
[centos@kingdom Desktop]$ [[ 7 > 5 || 6 > 5 ]] || echo "Hey"
[centos@kingdom Desktop]$ echo $?
0
[centos@kingdom Desktop]$ [[ 7 > 8 || 6 > 5 ]] || echo "Hey"
[centos@kingdom Desktop]$ echo $?
0
只有表达式为假时echo "Hey"才会被执行
在上面的例子中,&&,||就相当于一个条件语句,也可以把多个&&,||组合起来使用,如:
command1&&command2||command3,表示当command1返回真值时才执行command2,否则执行command3
第一个表达式为真,执行第二个命令
[centos@kingdom Desktop]$ [[ 7 > 8 || 6 > 5 ]] && echo "Hey"||ls -l
Hey
第一个表达式为假,执行第三个命令
[centos@kingdom Desktop]$ [[ 7 > 8 && 6 > 5 ]] && echo "Hey"||ls -l
total 40
-rw------- 1 centos centos 4534 Apr 24 21:59 kernel
-rw------- 1 centos centos 4534 Apr 24 21:48 kernel~
-rw-rw-r-- 1 centos centos 17512 Apr 24 20:29 kernel.txt
-rw-rw-r-- 1 centos centos 333 May 12 21:02 shift.txt
而对于表达式:
[[ expr1 && expr2 ]],只有expr1和expr2同为真时才为真
[[ expr1 || expr2 ]],只有expr1和expr2同为假时才为假
只是一个 “且” 或 “或” 的作用
阅读(815) | 评论(0) | 转发(0) |