当前位置:网站首页>Shell programming conditional statement test command Integer value, string comparison Logical test File test
Shell programming conditional statement test command Integer value, string comparison Logical test File test
2022-07-30 23:34:00 【1701y】
目录
Shell编程条件语句
条件测试操作
test命令
Used to test whether the expression holds,若成立返回0,否则返回其他数值
It can only be used to judge whether it is true or not,Can't tell if it's correct
格式1:test 条件表达式
格式2:[ 条件表达式 ] #Square brackets and conditional expressions must be surrounded by spaces,否则会报错

文件测试
[ 操作符 文件或目录 ] #There must be spaces between square brackets and content
常见的测试操作符
-d:测试是否为目录(Directory)
-e:测试目录或文件是否存在(Exist)
-f:测试是否为文件(File)
-r:测试当前用户是否有权限读取(Read)
-w:测试当前用户是否有权限写入(Write)
-x:测试当前用户是否有权限执行(eXcute
整数值比较
[ 整数1 操作符 整数2 ]
常见的测试操作符
-eq:等于(Equal)
-ne:不等于(Not Equal)
-gt:大于(Greater Than)
-lt:小于(Lesser Than)
-le:小于或等于(Lesser or Equal)
-ge:大于或等于(Greater or Equal)

字符串比较
格式1: [ 字符串1 = 字符串2 ] [ 字符串1 != 字符串2]
格式2:[ -z 字符串 ] [ -n 字符串 ]
常用的测试操作符
=:字符串内容相同
!=:字符串内容不同,!号表示取反的意思
-z:字符串内容为空 ,Variables that are not defined or assigned a null value will be treated as empty strings



逻辑测试
格式1:[ 表达式1 ] 操作符 [ 表达式2 ] ...
格式2:命令1 操作符 命令2 ...
常用的测试操作符
-a或&&:逻辑与,“而且”的意思,前后条件需都成立.
# &&,||操作符能够正常存在于[[]]条件判断结构中,但如果出现在[]结构中,会报错.
-o或||:逻辑或,“或者”的意思,Only one of the pre- and post-conditions needs to hold
!:逻辑否
&&

边栏推荐
猜你喜欢

$\text{ARC 145}$

47.【指针与数组】

Reverse linked list - in-place inversion method

详解操作符
Dry goods | 4 tips for MySQL performance optimization

Excel basic study notes

IJCAI2022 Tutorial | Spoken Language Comprehension: Recent Advances and New Fields

第十九周进度(了解物联网基础知识)

EasyExcel comprehensive course combat

Detailed operator
随机推荐
A detailed explanation: SRv6 Policy model, calculation and drainage
反转链表-头插反转法
Unity 加载读取PPT
ZZULIOJ:1119: 数列有序
Excel基础学习笔记
Computer shortcut icon whitening solution
flex-direction容器属性
2021GDCPC广东省大学生程序设计竞赛 H.History
el-upload添加请求头
Kotlin特殊类
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
"Wei cup" school more than 2022 cattle summer camp 4 Nancy (polocy) pelosi article variance law of Arts
MySQL的一个问题
Reverse linked list - in-place inversion method
Shell编程条件语句 test命令 整数值,字符串比较 逻辑测试 文件测试
HCIP第十五天笔记
MySQL面试题
Bugku sql注入
WebServer process explanation (registration module)
详解操作符