首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 23-11-18 13:41
  • 2885
  • 12006
blog.csdn.net

00. 目录

文章目录

    • 00. 目录
    • 01. 命令概述
    • 02. 命令格式
    • 03. 常用选项
    • 04. 参考示例
    • 05. 附录

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

05. 附录

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

注:本文转载自blog.csdn.net的沧海一笑-dj的文章"https://blog.csdn.net/dengjin20104042056/article/details/101200649"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top