← 返回首页
Linux高级程序设计(九)
发表时间:2021-11-04 21:28:21
条件测试语句-复合测试

1.复合测试

表达式/命令 含义
command1 && command2 command1返回0才执行command2
command1 || command2 command1返回非0才执行command2
condition1 -a condition2 两边条件都成立,返回0
condition1 -o condition2 两边条件任何一个成立,返回0
! condition 状态取反

实例: test.sh

#!/bin/bash
num=100
#命令控制方式写法
test $num -gt 0 && test $num -le 200
echo "num>0 && num<=200 =>$?"
#多重判断写法
test $num -gt 0 -a $num -le 200
echo "num>0 && num<=200 =>$?"
file="test.sh"
test -e $file -a -x $file
echo "test.sh file is exist and excuteable:$?"
s1="Hello"
test $s1 = "hello" -o $s1 = "Hello"
echo "s1 equals ignorse case 'hello':$?"

测试运行:

[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./test.sh
num>0 && num<=200 =>0
num>0 && num<=200 =>0
test.sh file is exist and excuteable:0
s1 equals ignorse case 'hello':0