00. 目录
01. 命令概述
test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
02. 命令格式
用法:
test EXPRESSION
test
- 1
- 2
- 3
03. 常用选项
#1. 关于两个整数之间的判定,例如 test n1 -eq n2
-eq 两数值相等 (equal)
-ne 两数值不等 (not equal)
-gt n1 大于 n2 (greater than)
-lt n1 小于 n2 (less than)
-ge n1 大于等于 n2 (greater than or equal)
-le n1 小于等于 n2 (less than or equal)
#2. 判定字符串
test -z string 判定字符串是否为 0 ?若 string 为空字符串,则为 true
test -n string 判定字符串是否非为 0 ?若 string 为空字符串,则为 false。
注: -n 亦可省略
test str1 = str2 判定 str1 是否等于 str2 ,若相等,则回传 true
test str1 != str2 判定 str1 是否不等于 str2 ,若相等,则回传 false
#3. 多重条件判定,例如: test -r filename -a -x filename
-a (and)两状况同时成立
例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才返回 true。
-o (or)两状况任何一个成立
例如 test -r file -o -x file,则 file 具有 r 或 x 权限时,就可返回 true。
! 逻辑非
如 test ! -x file ,当 file 不具有 x 时,返回 true
#4. 文件相关判断
test File1 –ef File2 两个文件是否为同一个文件,可用于硬连接。主要判断两个文件是否指向同一个inode。
test File1 –nt File2 判断文件1是否比文件2新
test File1 –ot File2 判断文件1比是否文件2旧
test –b file #文件是否块设备文件
test –c File #文件并且是字符设备文件
test –d File #文件并且是目录
test –e File #文件是否存在 (常用)
test –f File #文件是否为正规文件 (常用)
test –g File #文件是否是设置了组id
test –G File #文件属于的有效组ID
test –h File #文件是否是一个符号链接(同-L)
test –k File #文件是否设置了Sticky bit位
test –b File #文件存在并且是块设备文件
test –L File #文件是否是一个符号链接(同-h)
test –o File #文件的属于有效用户ID
test –p File #文件是一个命名管道
test –r File #文件是否可读
test –s File #文件是否是非空白文件
test –t FD #文件描述符是在一个终端打开的
test –u File #文件存在并且设置了它的set-user-id位
test –w File #文件是否存在并可写
test –x File #文件属否存在并可执行
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
04. 参考示例
4.1 判断文件是否存在
[deng@localhost ~]$ test test.txt
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
存在返回0,不存返回1
4.2 断目录是不是存在
[deng@localhost ~]$ test -d /home
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
4.3 判断文件是否有写的权限
[deng@localhost ~]$ test -w test.cpp
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
4.4 判断文件是否为空文件
[deng@localhost ~]$ test -s test.cpp
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
4.5 判断test.c是不是比test.cpp新
[deng@localhost ~]$ test test.c -nt test.cpp
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
4.6 判断2是不是等于3
[deng@localhost ~]$ test 2 -eq 3
[deng@localhost ~]$ echo $?
1
[deng@localhost ~]$
- 1
- 2
- 3
- 4
4.7 判断3是不是大于2
[deng@localhost ~]$ test 3 -gt 2
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
4.8 判断A是不是等于B
[deng@localhost ~]$ A="aaa"
[deng@localhost ~]$ B="bbb"
[deng@localhost ~]$ test A = B
[deng@localhost ~]$ echo $?
1
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
- 6
4.9 判断A是不是不等于B
[deng@localhost ~]$ A="aaa"
[deng@localhost ~]$ B="bbb"
[deng@localhost ~]$ test A != B
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
- 6
4.10 当home为目录,并且可写时为真
[deng@localhost ~]$ test -d /home -a -w /home
[deng@localhost ~]$ echo $?
1
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
评论记录:
回复评论: