@yanglt7
2018-10-24T14:06:40.000000Z
字数 3487
阅读 713
Shell
条件测试语法 |
---|
语法1:test <测试表达式> |
语法2:[<测试表达式>] |
语法3:[[<测试表达式>]] |
语法4:((<测试表达式>)) |
语法格式:test <测试表达式>
1)-f 选项(文件存在且为普通文件则表达式成立)
test -f file && echo true || echo false
2)一半逻辑
test -f file echo 1 #<== 若表达式成功,则输出1
test -f file echo 0 #<== 若表达式不成功,则输出0
3)示例
[root@web001 scripts]# test -f file && echo 1 || echo 0
0
[root@web001 scripts]# touch file
[root@web001 scripts]# test -f file && echo 1 || echo 0
1
4) -z 选项(如测试字符串的长度为0,则表达式成立)
[root@web001 scripts]# test -z "ylt" && echo 1 || echo 0
0
[root@web001 scripts]# test -z "" && echo 1 || echo 0
1
语法格式:[<测试表达式>] 中括号内两端需要空格
1)若文件 file 存在,则输出 true,否则输出 false。
[ -f file ] && echo true || echo false
2)一半逻辑
[ -f file] echo 1 #<== 若表达式成功,则输出1
[ -f file ] echo 0 #<== 若表达式不成功,则输出0
3)示例
[root@web001 scripts]# [ -f file ] && echo 1 || echo 0
0
[root@web001 scripts]# touch file
[root@web001 scripts]# [ -f file ] && echo 1 || echo 0
1
语法格式:[[<测试表达式>]] 双中括号内两端需要空格
1)若文件 file 存在,则输出 true,否则输出 false。
[[ -f file ]] && echo true || echo false
2)一半逻辑
[[ -f file ]] echo 1 #<== 若表达式成功,则输出1
[[ -f file ]] echo 0 #<== 若表达式不成功,则输出0
3)示例
[root@web001 scripts]# [[ -f file ]] && echo 1 || echo 0
0
[root@web001 scripts]# touch file
[root@web001 scripts]# [[ -f file ]] && echo 1 || echo 0
1
常用文件测试操作符 | 说明 |
---|---|
-d 文件,directory | 文件存在且为目录则为真,即测试表达式成立 |
-f 文件,file | 文件存在且为普通文件则为真,即测试表达式成立 |
-e 文件,exist | 文件存在则为真,即测试表达式成立,-e 不辨别文件还是目录 |
-r 文件,read | 文件存在且可读则为真,即测试表达式成立 |
-s 文件,size | 文件存在且文件大小不为 0 则为真,即测试表达式成立 |
-w 文件,write | 文件存在且可写则为真,即测试表达式成立 |
-x 文件,executable | 文件存在且可执行则为真,即测试表达式成立 |
-L 文件,link | 文件存在且为链接文件则为真,即测试表达式成立 |
-d 文件,directory | 文件存在且为目录则为真,即测试表达式成立 |
f1 nt f2,newer than | 文件 f1 比 文件 f2 新则为真,即测试表达式成立。根据文件的修改时间来计算 |
f1 ot f2,older than | 文件 f1 比 文件 f2 旧则为真,即测试表达式成立。根据文件的修改时间来计算 |
1)测试文件的读、写、执行等属性,不光是根据文件属性 rwx 的标识来判断,还要看当前执行测试的用户是否真的看按照对应的权限操作该文件。
[root@web001 scripts]# ll file
-rw-r--r-- 1 root root 0 Oct 24 03:57 file
[root@web001 scripts]#
[root@web001 scripts]# [ -r file ] && echo 1 || echo 0
1
[root@web001 scripts]# [ -w file ] && echo 1 || echo 0
1
[root@web001 scripts]# [ -x file ] && echo 1 || echo 0
0
[root@web001 scripts]# chmod 001 file
[root@web001 scripts]# ll file
---------x 1 root root 0 Oct 24 03:57 file
[root@web001 scripts]# [ -w file ] && echo 1 || echo 0
1
[root@web001 scripts]# echo "echo test" >file
[root@web001 scripts]# [ -r file ] && echo 1 || echo 0
1
[root@web001 scripts]# cat file
echo test
[root@web001 scripts]# [ -x file ] && echo 1 || echo 0
1
[root@web001 scripts]# ./file
test
2)[] 测试变量需要加双引号
[root@web001 scripts]# echo $ylt
[root@web001 scripts]# [ -f $ylt ] && echo 1 || echo 0
1
[root@web001 scripts]# [ -f "$ylt" ] && echo 1 || echo 0
0
3)特殊测试表达式
[ 条件1 ] && {
命令1
命令2
命令3
}
[[ 条件1 ]]
|| {
命令1
命令2
命令3
}
test 条件1 && {
命令1
命令2
命令3
}
相当于
if [ 条件1 ]
then
命令1
命令2
命令3
fi
常用字符串测试操作符 | 说明 |
---|---|
-n "字符串" | 若字符串的长度不为 0,则为真。 |
-z "字符串" | 若字符串的长度为 0,则为真。 |
"串1" = "串2" | 若字符串1等于字符串2,则为真 |
"串1" != "串2" | 若字符串1不等于字符串2,则为真 |
在 [] 以及 test 中使用的比较符号 | 在 [[]] 及 (()) 中使用的比较符号 | 说明 |
---|---|---|
-eq | == 或 = | 相等,equal |
-ne | != | 不相等,not equal |
-gt | > | 大于,greater than |
-ge | >= | 大于等于,greater equal |
-lt | < | 小于,less than |
-le | <= | 小于等于,less equal |
在 [] 以及 test 中使用的操作符 | 在 [[]] 及 (()) 中使用的操作符 | 说明 |
---|---|---|
-a | && | and,与,两端都为真,则结果为真 |
-o | || | or,或,两端有一个为真,则结果为真 |
! | ! | not,非,两端相反,则结果为真 |
- 连接两含 [] 、test 或 [[]] 的表达式可用 && 或 ||。
- 在 [[]] 通配符匹配的场景下,其他测试表达式无法替代。因此,如果需要通配符或正则表达式就用 [[]]。
测试表达式符号 | [] | test | [[]] | (()) |
---|---|---|---|---|
边界是否需要空格 | 需要 | 需要 | 需要 | 不需要 |
逻辑操作符 | !、-a、-o | !、-a、-o | !、&&、|| | !、&&、|| |
整数比较操作符 | -eq、-ne、-gt、-lt、-ge、-le | -eq、-ne、-gt、-lt、-ge、-le | -eq、-ne、-gt、-lt、-ge、-le 或 =、>、<、>=、<= | =、>、<、>=、<= |
字符串比较操作符 | =、==、!= | =、==、!= | =、==、!= | =、==、!= |
是否支持通配符匹配 | 不支持 | 不支持 | 支持 | 不支持 |