Linux test命令

发布时间:2025-08-11 05:12

学习Linux命令行:'The Linux Command Line' 是经典教材 #生活技巧# #工作学习技巧# #编程学习资源#

Linux test命令:检查文件类型,并比较值。

Linux test命令 功能描述

使用test命令可以检查文件类型,并比较值。

Linux test命令 语法

test [表达式] [选项]

Bash

命令中各选项的含义如下表所示。

test命令选项含义

在使用test命令时,可以指定下表所示的表达式。

Linux test命令 语法

Linux test命令 示例

字符串比较

[root@rhel ~]# str1=abcd [root@rhel ~]# test str1=abcd [root@rhel ~]# echo? 0 //结果显示0表示字符串str1确实等于abcd

Bash

数字相等比较

[root@rhel ~]# int1=1234 [root@rhel ~]# int2=01234 [root@rhel ~]# test int1-eqint2 [root@rhel ~]# echo $? 0 //结果显示0表示字符int1和int2比较,二者值一样大

Bash

数字大于比较

[root@rhel ~]# int1=4 [root@rhel ~]# test int1-gt 2 [root@rhel ~]# echo? 0 //结果显示0表示字符int1的值确实大于2

Bash

逻辑测试

[root@rhel ~]# test -r empty -a -s empty [root@rhel ~]# echo $? 1 //结果显示1表示文件empty存在且只读及长度为0

Bash

文件操作测试

[root@rhel ~]# cat /dev/null>empty [root@rhel ~]# cat empty [root@rhel ~]# test -r empty [root@rhel ~]# echo ? 0 //结果显示0表示文件empty存在且只读 [root@rhel ~]# test ! -s empty [root@rhel ~]# echo? 0 //结果显示0表示文件empty存在且文件长度为0

Bash

网址:Linux test命令 https://klqsh.com/news/view/136393

相关内容

Linux ed命令
快速了解Linux中的网速测试命令! – Linux命令大全(手册)
Shell test命令(Shell [])详解,附带所有选项及说明
高效利用Linux命令行在线收听网络广播电台
Linux speedtest
linux服务器测试网速的方法
Linux软件测试求职者必读:打好面试基础!(linux软件测试面试)
使用 Kubernetes 联邦(Kubefed)进行多集群管理
Alpine Linux和armbian的性能占用对比
Get the Postman app

随便看看