当前位置:网站首页>Fundamentals of shell programming (Chapter 9: loop)
Fundamentals of shell programming (Chapter 9: loop)
2022-07-05 19:19:00 【Just call me Wang Yuanwai】
Preface
In life, we always do the same thing , Loop simulation is used in the program to do repetitive things , Any language has circular statements , stay shell Programming , Support 3 A circular grammar , Namely while、for、for in!
while Two kinds of grammar
1、 On the same line
while condition; do
command
donewhile And do On the same line , Use ; separate ,while Every time I judge condition Exit status code , If 0, Then execute the code of the loop body
2、 Not on the same line
while condition
do
command
donewhile And do Not in the same line , There is no need for ;
In a single line
while true; do echo 'hello world'; doneUse ; Just separate
Only judge the exit status code
while true; do
echo 'hello world';
donebecause true The exit status code of this command is always 0, So the cycle will continue . Any command with exit status code can be placed in while Behind , They could be
1、 function
2、 A command
3、 Another script program
wait , As long as there is an exit status code , As a condition
Multiple conditions , Only recognize the last condition
while true; false; do
echo 'hello world';
doneThe same line , Only recognize the exit status code of the last condition
while summary
1、 Almost if The standards are the same , The condition is to exit the status code , therefore test Commands are often used as conditions
2、while The utilization rate of is quite high , Please learn it
for in grammar
1、 On the same line
for item in hello world; do
echo $item
done
2、 Not on the same line
for item in hello world
do
echo $item
done
list
stay for in In the syntax ,in Followed by a list , And the list is blank ( Space 、 Line break 、 Tabulation 、 Any character ) A list of separated words , In circulation , The words in the list will be traversed one by one , So we can often use the return value of command substitution , Traversal , For example, traverse every file in the current working directory
for file in $(ls);do
echo $file
done
Write in a line
for item in hello world;do echo $item; doneReplace with file name
for i in *.png; do
echo $i
donefor in summary
1、 Remember one ,in Followed by a list of words separated by white space characters ( Be careful :IFS You can modify the default separator )
for grammar
class C Language innovation is the ancestor of computer high-level language ,shell Programming is also learned C Linguistic for grammar
for (( i=0; i<5; i=i+1 )); do
echo $i
doneGrammar , Double brace , ha-ha , Can't hide !! In double parentheses , Accessing variable values , Even the dollar sign can be omitted !!
Infinite loop
for ((;;))
do
echo hello world
sleep 5
doneFinal summary
1、shell Programming , Support while、for in、for,3 There are two ways of circulation
2、 In fact, there is another kind until, Because it's not often used , This article will not mention
3、 No matter what language , Follow its standard syntax , Learn to get twice the result with half the effort !!!!
边栏推荐
- Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
- Hiengine: comparable to the local cloud native memory database engine
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]
- PG basics -- Logical Structure Management (user and permission management)
- 数学分析_笔记_第9章:曲线积分与曲面积分
- PG基础篇--逻辑结构管理(用户及权限管理)
- Apprentissage du projet MMO I: préchauffage
- 自动化测试的好处
- Shell编程基础(第8篇:分支语句-case in)
猜你喜欢

Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?
PHP利用ueditor实现上传图片添加水印

What are the reliable domestic low code development platforms?

数据库 逻辑处理功能

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

JAD的安装、配置及集成IDEA

Fuzor 2020软件安装包下载及安装教程

【AI 框架基础技术】自动求导机制 (Autograd)
The problem of returning the longtext field in MySQL and its solution

UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
随机推荐
Debezium系列之:修改源码支持drop foreign key if exists fk
#夏日挑战赛# HarmonyOS - 实现消息通知功能
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
2022最新大厂Android面试真题解析,Android开发必会技术
出海十年:新旧接力,黑马崛起
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
泰山OFFICE技术讲座:由行的布局高度,谈绘制高度的高度溢出、高度缩水(全网首发)
Teach you to deal with JS reverse picture camouflage hand in hand
测试外包公司怎么样?
Django使用mysqlclient服务连接并写入数据库的操作过程
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
开源 SPL 消灭数以万计的数据库中间表
C# 语言的高级应用
Hiengine: comparable to the local cloud native memory database engine
Postman核心功能解析 —— 参数化和测试报告
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
100million single men and women supported an IPO with a valuation of 13billion