当前位置:网站首页>Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
2022-07-28 06:57:00 【Fish fire loach】
shell Script —— Programming conditional statement ( Conditions for testing 、if sentence 、case Branch statement )
One 、 Conditions for testing
1、test test
test expression Is it true , Return if established 0, Otherwise, other values are returned
Be careful Space Space Space
Format 1:test Conditional expression

2、 File test
Format
[ The operator File or directory ]


3、 Integer test
Integer comparison
[ Integers 1 The operator Integers 2 ]
| -eq | be equal to equal |
| -ne | It's not equal to not equal |
| -gt | Greater than greater than |
| -lt | Less than lesser than |
| -le | Less than or equal to lesser or equal |
| -ge | Greater than or equal to greater or equal |

4、 String test
String comparison
Format 1:[ character string 1 = character string 2 ]
Format 2:[ character string 1 != character string 2 ]
Format 3:
[ -z character string ] # Check if the string is empty zero, Variables that are undefined or given null values are treated as empty strings
[ -n character string ] # Check if there is a string


( 5、 ... and ) Logic test 

Two 、if sentence
1、if Single branch statements



2、if Two branch statements

Example 

3、if Multi branch statement

Example 

3、 ... and 、case Branch statement ( Like a menu , There are options to use CASE)

Example


Four 、echo command - Output character or extract shell The value of the variable
1、 Format
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-0lrMXJd2-1657181718542)(C:\Users\YU\AppData\Roaming\Typora\typora-user-images\image-20220706115343958.png)]
2、 Common parameters

5、 ... and 、for loop
Read different values of variables , Used to execute the same set of commands one by one

Example



Example : Output 20 Even numbers within




notes :i++ : i=1+1 Assign first and then calculate i=1 after Again +1
++i : 1+1=i Operation before assignment 1+1 after Again =i
6、 ... and 、while loop
As long as the conditions are met , And then it goes back and forth , Not immediately

1、 Grammatical structure
while expression
do
command
done
2、 Example
Print 1-5 The number of

notes : Dead cycle , Output all the time 1

notes : Not true, so output

7、until loop
Test a condition repeatedly , As long as the condition is not true, it will be repeated

Example

[ Outside the chain picture transfer in …(img-OBiujmZI-1657181718545)]
7、until loop
Test a condition repeatedly , As long as the condition is not true, it will be repeated
[ Outside the chain picture transfer in …(img-QPgIsqpq-1657181718545)]
Example
[ Outside the chain picture transfer in …(img-h6Rtpc5f-1657181718546)]

边栏推荐
- MySQL master-slave
- Wechat applet custom compilation mode
- 修复故障扇区
- Tcp/ip five layer model
- 技术分享 | 实战详解接口测试请求方式Get、post
- Vmware workstation configuration net mode
- archery数据库审核平台部署
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset
- shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
猜你喜欢
![[explain in detail how to realize Sanzi chess step by step]](/img/17/68ef51ec2be0c86019461116ecaa82.png)
[explain in detail how to realize Sanzi chess step by step]

Ten thousand words summarize and realize the commonly used sorting and performance comparison

Vmware workstation configuration net mode

一、PXE概述和安装

What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset

Centos7 deploy MySQL database server

Installation and configuration of unit test framework jest with typescript

QGraphicsView提升为QChartView

Dynamic memory management function of C language

What kind of air conduction Bluetooth headset with good configuration is recommended
随机推荐
Array to linked list
C language memcpy library functions and the role of memmove
软件测试的生命周期(流程)
JS four operations are repackaged to solve the problem of precision loss
Detailed explanation of LNMP construction process
Technology sharing | send requests using postman
How to describe a bug and the definition and life cycle of bug level
NAT和PAT的原理及配置
QT uses MSVC compiler to output Chinese garbled code
File operation in C language
Tcp/ip five layer model
QT使用MSVC编译器输出中文乱码问题
Which is the best and most cost-effective air conduction headset recommended
How to simulate the implementation of strcpy library functions
NFS 共享存储服务
DNS domain name resolution service
Wechat applet custom compilation mode
How to store floating point data in memory
HDU-1159-CommonSubsequence(LCS最长公共子序列)
Pku-2739-sum of constructive prime numbers