【Linux】一步一步学Linux——test命令(252)

00. 目录

01. 命令概述

test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

02. 命令格式

用法:
       test EXPRESSION
       test

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   #文件属否存在并可执行

04. 参考示例

4.1 判断文件是否存在

[deng@localhost ~]$ test test.txt 
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$ 

存在返回0,不存返回1

4.2 断目录是不是存在

[deng@localhost ~]$ test -d /home
[deng@localhost ~]$ echo $? 
0
[deng@localhost ~]$ 

4.3 判断文件是否有写的权限

[deng@localhost ~]$ test -w test.cpp
[deng@localhost ~]$ echo $? 
0
[deng@localhost ~]$ 

4.4 判断文件是否为空文件

[deng@localhost ~]$ test -s test.cpp 
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$ 

4.5 判断test.c是不是比test.cpp新

[deng@localhost ~]$ test test.c -nt test.cpp
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$ 

4.6 判断2是不是等于3

[deng@localhost ~]$ test 2 -eq 3 
[deng@localhost ~]$ echo $? 
1
[deng@localhost ~]$ 

4.7 判断3是不是大于2

[deng@localhost ~]$ test 3 -gt 2 
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$ 

4.8 判断A是不是等于B

[deng@localhost ~]$ A="aaa"
[deng@localhost ~]$ B="bbb"
[deng@localhost ~]$ test A = B
[deng@localhost ~]$ echo $? 
1
[deng@localhost ~]$ 

4.9 判断A是不是不等于B

[deng@localhost ~]$ A="aaa"
[deng@localhost ~]$ B="bbb"
[deng@localhost ~]$ test A != B
[deng@localhost ~]$ echo $?
0
[deng@localhost ~]$ 

4.10 当home为目录,并且可写时为真

[deng@localhost ~]$ test -d /home -a -w /home
[deng@localhost ~]$ echo $? 
1
[deng@localhost ~]$ 

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值