当前位置:网站首页>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
!:逻辑否
&&

边栏推荐
- 机器学习1一回归模型(二)
- Apache Doris系列之:深入认识实时分析型数据库Apache Doris
- flex布局父项常见属性flex-wrap
- Manually set transaction commit in mysql
- [0x800706D9] solution appears in Microsoft Store
- JS中? ?和??=和?.和 ||的区别
- H5跳转微信公众号解决方案
- WebServer process explanation (registration module)
- Apache Doris系列之:安装与部署详细步骤
- "NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 DHKLN
猜你喜欢
随机推荐
EasyExcel comprehensive course combat
Shell脚本 if语句
How to open the payment channel interface?
47.【指针与数组】
The problem of sticky packets in tcp protocol transmission
WebServer流程讲解(注册模块)
“蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
#Dasctf July Enabler WP
2022牛客暑期多校训练营1 J Serval and Essay
软考总结
$\text{ARC 145}$
uniapp折叠框二级循环
leetcode 406. Queue Reconstruction by Height
“蔚来杯“2022牛客暑期多校训练营4 L.Black Hole 垃圾计算几何
2D转换模块&&媒体查询
"Wei cup" school more than 2022 cattle summer camp 4 L.B lack Hole, computational geometry
Week 19 Progress (Understanding IoT Basics)
Introducing the visualization tool Netron
Day016 类和对象
“蔚来杯“2022牛客暑期多校训练营4 N.Particle Arts 规律 方差









