总结
01、test <测试表达式> 条件测试表达式
02、[<测试表达式>] 条件测试表达式,常用
03、[[<测试表达式>]] 条件测试表达式,正则匹配
04、((<测试表达式>)) 条件测试表达式,常用于if语句
05、char="old";test -z "$char" && echo 1||echo 0
06、test -f /tmp/oldboy.txt && echo 1
07、[ -f /tmp/oldboy.txt ]&&echo 1||echo 0
08、[[ -f /tmp/oldboy.txt ]]&&echo 1||echo 0
09、-d directory 文件存在且为目录则为真,则表达式成立
10、-f file 文件存在且为普通文件则为真,则表达式成立
11、-e exist 文件存在则为真,则表达式成立
12、-r read 文件存在且可读则为真,则表达式成立
13、-w write 文件存在且可写则为真,则表达式成立
14、-x executable 文件存在且可执行则为真,则表达式成立
15、-s size 文件存在且大小不为0则为真,则表达式成立
16、-L link 文件存在且为链接文件则为真,则表达式成立
17、f1 -nt f2 newer than f1比f2新则为真,则表达式成立
18、f1 -ot f2 older than f1比f2旧则为真,则表达式成立
19、touch oldboy;[ -f oldboy ]&&echo 1||echo 0
20、mkdir oldboy;[ -d oldboy ]&&echo 1||echo 0
21、[ -e old ]&&echo 1||echo 0
22、[ -r oldboy ]&&echo 1||echo 0
23、[ -w oldboy ]&&echo 1||echo 0
24、[ -x oldboy ]&&echo 1||echo 0
25、f1=/etc/services;f1=/etc/rc.local;[ -f "$f1" ]&&echo 1||echo 0
26、[ -f /etc/sysconfig/network ]&& . /etc/sysconfig/network
27、sed -n '44,50p' /etc/init.d/nfs
28、特殊条件测试表达式:cat 1.sh
[ -f /etc ]||{
echo 1
echo 2
echo 3
}
29、-n "字符串" 字符串长度不为0,则为真
30、-z "字符串" 字符串长度为0,则为真
31、"字符串1"="字符串2" 字符串1等于字符串2,则为真
33、"字符串1"!="字符串2" 字符串1不等于字符串2,则为真
33、字符串测试表达式:[ -n "abc" ]&&echo 1||echo 2
33、var=abc;[ -z "$var" ]&&echo 1||echo 2
35、[ "abc" = "abc" ]&&echo 1||echo 2
36、[ "abc"!="abc" ]&&echo 1||echo 2
37、sed -n '30,31p' /etc/init.d/network
38、-eq == equal 相等
39、-ne != not equal 不相等
40、-gt > greater than 大于
41、-ge >= greater equal 大于等于
44、-lt < less than 小于
44、-le <= less equal 小于等于
44、整数二元比较:[ 2 > 1 ]&&echo 1||echo 0
45、[ 2 -gt 1 ]&&echo 1||echo 0
46、[[ 2 > 1 ]]&&echo 1||echo 0
47、[[ 2 -lt 1 ]]&&echo 1||echo 0
48、((3>2))&&echo 1||echo 0
49、a=98;b=99;[ $a -eq $b ]&&echo 1||echo 2
50、[[ $a > $b ]]&&echo 1||echo 2
51、(($a>$b))&&echo 1||echo 2
55、整数变量测试:grep -w "\-eq" /etc/init.d/nfs
55、-a && and 与
55、-o || or 或
55、! ! not 非
56、[ 5 -eq 6 -o 5 -gt 3 ]&&echo 1||echo 0
57、f1=/etc/services;f1=/etc/rc.local;[ -f "$f1" -a -f "$f2" ]&&echo 1||echo 0
58、[[ ! -n "$f1" && "$f2" = "$f1" ]]&&echo 1||echo 0
浙公网安备 33010602011771号