Linux Shell 脚本编程和其他编程语言一样,支持算数、关系、布尔、逻辑、字符串、文件测试等多种运算。前面几节我们陆续研究了 Linux shell编程 中的 字符串运算、算术运算和关系运算,今天我们来研究 Linux shell编程中的的布尔运算、逻辑运算。
一、布尔运算
(一)布尔运算符功能说明
运算符 | 说明 | 备注 |
! | 非运算,表达式为 true 则返回 0(false),否则返回 1(true)。 | not |
-o | 或运算,有一个表达式为 true 则返回 1(true),否则返回 0(false)。 | or |
-a | 与运算,两个表达式都为 true 才返回1(true),否则返回 0(false)。 | and |
(二)布尔运算实例
1.操作数为数值
user @ host : ~ $ a=1
user @ host : ~ $ b=-1
user @ host : ~ $ c=3
user @ host : ~ $ if [ ! $a ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ ! $b ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ ! $b == $a ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ $b == $a -o $b ==$c ]; then echo "true"; else echo "false"; fi
bash: [: -1: 需要一元表达式
false
user @ host : ~ $ if [ $b == $a -o $b == $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -a $b == $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -o $b == $b ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ $b == $a -a $b == $b ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $
2.操作数为字符串
user @ host : ~ $ a="a"
user @ host : ~ $ b="b"
user @ host : ~ $ c="c"
user @ host : ~ $ if [ ! $a ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -o $b == $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -a $b == $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -o $b == $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -a $b == $b ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $b == $a -o $b == $b ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $
3.操作数为true 和 false
user @ host : ~ $ c=true
user @ host : ~ $ if [ $c ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ ! $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ f=false
user @ host : ~ $ if [ $f ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ ! $f ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [ $c -o $f ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ $c -a $f ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ $f -a $c ]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [ ! $f -a $c ]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ echo $f
false
user @ host : ~ $ echo $c
true
user @ host : ~ $ echo `expr $f == true `
0
user @ host : ~ $ echo `expr $c == true `
1
user @ host : ~ $ echo `expr $c == false `
0
user @ host : ~ $ echo `expr $f == false `
1
user @ host : ~ $
比较奇怪的是,对于变量f,我们初始化其值为false后,执行以下命令的结果。
(1)命令:
if [ $f ]; then echo "true"; else echo "false"; fi
执行结果是: true
(2)命令:
if [ ! $f ]; then echo "true"; else echo "false"; fi
执行结果是: false
(3)尽管变量c的值为true,但变量f的值为false,所以 表达式$c -a $f 的值应为false
但命令: if [ $c -a $f ]; then echo "true"; else echo "false"; fi
执行结果是:true
之所以出现这些问题,是因为 Linux shell脚本编程中,虽然有布尔运算符和布尔运算,但不能直接将 true 或 false 作为布尔值赋给一个变量!
其实在上面的实例中,命令 c=true 是将 字符串 true 而不是布尔值true 赋给变量c
同样的,命令 f=false 是将 字符串 false 而不是布尔值 false 赋给变量f
所以,上面这个实例中操作数也是字符串!
二、逻辑运算
(一)逻辑运算符功能说明
运算符 | 说明 |
&& | 逻辑的 AND |
|| | 逻辑的 OR |
这些运算符与汇编语言和C/C++/JavaScript 等语言是一样的。
(二)逻辑运算实例
一般表达式运算只用1个对[],而逻辑去处要用2对[]。
1.操作数为 true 和 false
user @ host : ~ $ c=true
user @ host : ~ $ f=false
user @ host : ~ $ if [ $c && $f ]; then echo "true"; else echo "false"; fi
bash: [: 缺少 `]'
false
user @ host : ~ $ if [[ $c && $f ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [[ $c || $f ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [[ $c || ! $f ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [[ ! $c || ! $f ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [[ ! $c || $f ]]; then echo "true"; else echo "false"; fi
trueuser @ host : ~ $ if [[ ! $c && $f ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [[ $c && ! $f ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $
以上命令中,c=true, f=false,有些命令的执行结果比较奇怪。
比如 表达式 $c && $f 的值应该是false,但比较奇怪的是命令:
if [[ $c && $f ]]; then echo "true"; else echo "false"; fi
执行结果是:true
究其原因,跟上面说的一样,命令c=true, f=false,所赋值的类型都是字符串,则不是布尔型。
2.操作数为数值
user @ host : ~ $ c=1
user @ host : ~ $ f=3
user @ host : ~ $ if [ $c && $f ]; then echo "true"; else echo "false"; fi
bash: [: 缺少 `]'
false
user @ host : ~ $ if [[ $c && $f ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ c=0
user @ host : ~ $ if [[ $c && $f ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ b=-1
user @ host : ~ $ if [[ $c -gt $b && $f -gt $b ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [[ $b -gt $c && $b -gt $f ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [[ $b -gt $c || $b -gt $f ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $
3.操作数为字符串
user @ host : ~ $ a="a"
user @ host : ~ $ b="b"
user @ host : ~ $ c="c"
user @ host : ~ $ if [[ $a == $b && $a == $c ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [[ $a == $b || $a == $c ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $ if [[ $a != $b || $a == $c ]]; then echo "true"; else echo "false"; fi
true
user @ host : ~ $ if [[ $a != $b && $a == $c ]]; then echo "true"; else echo "false"; fi
false
user @ host : ~ $