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
二、逻辑运算
(一)逻辑运算符功能说明
运算符 | 说明 |
---|---|
&& | 逻辑的 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
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 : ~ $