当前位置:网站首页>Shell programming of conditional statements
Shell programming of conditional statements
2022-07-31 00:23:00 【white for superman】
Conditional test
The Shell environment judges whether the execution is successful according to the return status value " $? " after the command is executed. When the return value is 0, it means success, otherwise it means failure or exception (non-zero value).
Using the special test tool test command, you can test a specific condition and judge whether it is true or not according to the return value (value is 0).
test conditional expression
[Conditional Expression] #Both sides of the conditional expression need to have spaces, no spaces will report an error, and this method is more commonly used
[[ conditional expression ]]
Options:
-a Whether the file exists (-a will be a problem if it is negated, it is recommended to use -espan>)
-e file exists
-f to judge whether it is a normal file
-d determines whether it is a directory file
-r determines whether the current user can read the file
-w determines whether the current user can write to the file
-x determines whether the current user can execute the file
ps: The difference between [ ] and [[ ]] [] does not support regular nuclear wildcards [[]] supports
All permissions for super administrators:Except x, other discomforts are limitedp>
1. Others do not have rw super admins do
2. Others don't have x super administrators either, as long as other users have x super administratorsMembers also have
Integer comparison
General formula: [ integer1 operator integer2 ]
Operator:
-eq equals
-ne is not equal to
-lt is less than
-gt greater than
-le less than or equal to
-ge greater than or equal to
String comparison
Format:
[string1=string2]
[string1!=string2]
[ -z string ]
Common test operators:
=: The first string is the same as the second string
!=: The first string is not the same as the second string, where the "!" symbol means negation
-z: empty string content
Logic Test
Logical testing refers to determining the dependencies between two or more conditions.When the system task depends on several different conditions, it is necessary to have a testing process to judge whether these conditions are established at the same time or only one of them is established.
Format:
[ expression 1 ] operator [ expression 2 ]
Command 1 Operator Command 2
Common test operators:
&&: Logical AND, which means that the return value of the entire test command is 0 (the result is true) only when both conditions are true.
When using the Test command to test, "&&" can be changed to "-a".
||: Logical OR, indicating OR, as long as one of the two conditions before and after is established, the return value of the entire test command is 0 (the result is established).
When using the Test command to test, "||" can be changed to "-o".
!: Logical NO, indicating "no", only when the specified condition is not met, the return value of the entire test command is 0 (the result is established).
Short-circuit and (and): &&
All true is true, and false is false
cmd1 && cmd2
1. The first command is false and there is no need to execute the command 2 should be positivefalse
2. The first command is true and I need to execute command 2 before I can judge whether it isTrue
Short or (or): ||
A true is true, all false is false
cmd1 || cmd2
1. The first command is true, no need to execute command 2, it should be affirmativetrue
2. The first command is false, and the whole formula will be judged after executing command 2Is the child true
if:Conditional judgment statement
To be continued
边栏推荐
- registers (assembly language)
- 加密传输过程
- Error occurred while trying to proxy request The project suddenly can't get up
- In-depth understanding of the auto-increment operator from two error-prone written test questions
- The difference between h264 and h265 decoding
- 【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
- [In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
- @requestmapping注解的作用及用法
- 作业:iptables防止nmap扫描以及binlog
- MySQL笔记下
猜你喜欢
数据库的严格模式
Machine Learning 1-Regression Model (2)
如何在WordPress网站上添加导航菜单
Shell programming conditional statement test command Integer value, string comparison Logical test File test
Mysql体系化之JOIN运算实例分析
pytorch双线性插值
Bypass of xss
Oracle一个诡异的临时表空间不足的问题
IOT跨平台组件设计方案
.NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
随机推荐
The difference between truncate and delete in MySQL database
消息队列存储消息数据的MySQL表设计
【Multithreading】
在微服务中使用事件溯源的六大原因 - Herath
MySQL grant statements
乌克兰外交部:乌已完成恢复粮食安全出口的必要准备
A Brief Talk About MPI
joiplay模拟器报错如何解决
background对float的子元素无效
unity2D横版游戏教程4-物品收集以及物理材质
How to Repair Word File Corruption
(五)fastai应用
asser利用蚁剑登录
The difference between substring and substr in MySQL
软件开发设计流程
joiplay模拟器rtp如何安装
47. 【Pointers and Arrays】
Learn Scope from a Compilation Perspective!
Strict Mode for Databases
binglog log tracking: data backup and backup tracking