linux test命令测试标志
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
·
test命令可以用于检查某个条件是否成立,可以分为数值、字符串、文件、变量四个方面,其中文件的测试包括文件类型、文件权限的判断和两个文件之间的比较。
数值
| 测试标志 | 意义 | 示例 |
| -eq | 两数相等 | test n1 -eq -n2,n1等于n2返回true。 |
| -ne | 两数不等 | test n1 -ne -n2,n1不等于n2返回true。 |
| -gt | 前者大于后者 | test n1 -gt -n2,n1大于n2返回true。 |
| -lt | 前者小于后者 | test n1 -lt -n2,n1小于n2返回true。 |
| -ge | 前者大于等于后者 | test n1 -ge -n2,n1大于等于n2返回true。 |
| -le | 前者小于等于后者 | test n1 -le -n2,n1小于等于n2返回true。 |
字符串
| 测试标志 | 意义 | 示例 |
| -z | 字符串长度为0 | test -z str1,str1长度为0,返回true。 |
| -n、string | 字符串长度不为0 | test -n str2或test str2,str2长度不为0,返回true。 |
| =、== | 两字符串相等 | test str1 = str2,str1与str2相等,返回true。 |
| != | 两字符串不相等 | test str1 != str2,str1与str2不相等,返回true。 |
| < | 比较字符串字典顺序 | test str1 < str2,按照字典顺序str1在str2之前,返回true。 |
| > | 比较字符串字典顺序 | test str1 > str2,按照字典顺序str1在str2之后,返回true。 |
文件
| 测试标志 | 意义 | 示例 |
| -e、-a | 文件名是否存在 | test -e filename,存在文件名filename,返回true。 |
| -f | 文件名是否存在且为文件 | test -f filename,存在文件名filename且为文件,返回true。 |
| -d | 文件名是否存在且为目录 | test -d filename,存在文件名filename且为目录,返回true。 |
| -b | 文件名是否存在且为一个block device设备 | test -b filename,存在文件名filename且为一个block device设备,返回true。 |
| -c | 文件名是否存在且为一个character device设备 | test -c filename,存在文件名filename且为character device设备,返回true。 |
| -S | 文件名是否存在且为一个Socket文件 | test -S filename,存在文件名filename且为一个Socket文件,返回true。 |
| -p | 文件名是否存在且为一个FIFO(pipe)文件 | test -p filename,存在文件名filename且为FIFO(pipe)文件,返回true。 |
| -L | 文件名是否存在且为一个连接文件 | test -L filename,存在文件名filename且为一个连接文件,返回true。 |
| -r | 文件名是否存在且具有“可读”权限 | test -r filename,存在文件名filename且具有“可读”属性,返回true。 |
| -w | 文件名是否存在且具有“可写”权限 | test -w filename,存在文件名filename且具有“可写”属性,返回true。 |
| -x | 文件名是否存在且具有“可执行”权限 | test -x filename,存在文件名filename且具有“可执行”属性,返回true。 |
| -u | 文件名是否存在且具有“SUID”属性 | test -u filename,存在文件名filename且具有“SUID”属性,返回true。 |
| -g | 文件名是否存在且具有“SGID”属性 | test -g filename,存在文件名filename且具有“SGID”属性,返回true。 |
| -k | 文件名是否存在且具有“Sticky bit”属性 | test -k filename,存在文件名filename且具有“Sticky bit”属性,返回true。 |
| -s | 文件名是否存在且为“非空白文件” | test -s filename,存在文件名filename且为非空白文件,返回true。 |
| -nt | 前者是否比后者新(根据修改日期) |
test filename1 -nt filename2,filename1比filename2新或者filename1存在filename2不存在,返回true。 |
| -ot | 前者是否比后者旧(根据修改日期) | test filename1 -ot filename2,filename1比filename2旧或者filename2存在filename1不存在,返回true。 |
| -ef | 是否为同一文件,可用在硬链接上。判断两个文件是否指向同一个inode | test filename1 --ef filename2,filename1与filename2为同一文件,返回true。 |
| -h、-L | 文件存在且是符号链接 | test -h filename,存在文件名为filname且为符号链接,返回true。 |
| -t | 文件描述符打开并指向终端 | test -t fd,如果文件描述符fd打开并指向终端,返回true。 |
| -G | 文件存在且属于有效组id | test -G filename,存在文件名为filname,且属于有效组id,返回true。 |
| -N | 文件存在且在最后一次读取后有修改 | test -N filename,存在文件名为filname,且在最后一次读取后有修改,返回true。 |
| -O | 文件存在且属于有效用户id | test -O filename,存在文件名为filname,且属于有效用户id,返回true。 |
多重条件
| 测试标志 | 意义 | 示例 |
| -a | 两个条件同时成立 | test -r filename -a -w filename,filename存在且同时具有读写权限,返回true。 |
| -o | 任何一个条件成立 | test -r filename -o -w filename,filename存在且具有读或写权限,返回true。 |
| ! | 取反 | test ! -x filename ,filename存在且不具有执行权限,返回true。 |
变量
| 测试标志 | 意义 | 示例 |
| -o | shell内建命令set的-o选项是否启用 | |
| -v | shell变量是否已赋值 | |
| -R |
shell变量是否已赋值并且是名称引用 |
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)