shell 测试命令

本文介绍了如何使用test命令对字符串、整数及文件进行条件测试。包括字符串相等、不相等、非空测试;整数相等、不相等、大小比较;文件是否为目录、常规文件、可执行、可读、可写、存在与否及文件大小是否非零的测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用 test 命令可以对文件、字符串等进行测试,一般配合控制语句使用。

1、字符串测试

test str1 = str2    //测试字符串是否相等
test str1 != str2    //测试字符串是否不相等
test str1    //测试字符串是否部位空
test -n str1    //测试字符串是否部位空
test -z str1    //测试字符串是否为空

2、整数测试

test int1 -eq int2    //测试整数是否相等
test int1 -ne int2    //测试整数是否不相等
test int1 -gt int2    //测试 int1 是否 > int2
test int1 -ge int2    //测试 int1 是否 >= int2
test int1 -lt int2    //测试 int1 是否 < int2
test int1 -le int2    //测试 int1 是否 <= int2

3、文件测试

test -d file    //指定文件是否是目录
test -f file    //指定文件是否是常规文件
test -x file    //指定文件是否可执行
test -r file    //指定文件是否可读
test -w file    //指定文件是否写
test -a file    //指定文件是否存在
test -s file    //文件大小是否不是 0          

 

转载于:https://www.cnblogs.com/fangwu/p/8422910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值