当前位置:网站首页>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
done
while 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
done
while And do Not in the same line , There is no need for ;
In a single line
while true; do echo 'hello world'; done
Use ; Just separate
Only judge the exit status code
while true; do
echo 'hello world';
done
because 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';
done
The 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; done
Replace with file name
for i in *.png; do
echo $i
done
for 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
done
Grammar , 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
done
Final 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 !!!!
边栏推荐
- JS解力扣每日一题(十二)——556. 下一个更大元素 III(2022-7-3)
- Talking about fake demand from takeout order
- Mariadb root用户及普通用户的密码 重置
- Go语言 | 02 for循环及常用函数的使用
- 集合
- Taishan Office Technology Lecture: from the layout height of the line, talk about the height overflow and height shrinkage of the drawing height (launched in the whole network)
- 测试的核心价值到底是什么?
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- golang通过指针for...range实现切片中元素的值的更改
- Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
猜你喜欢
Hiengine: comparable to the local cloud native memory database engine
Why can't Bi software do correlation analysis? Take you to analyze
Reflection and imagination on the notation like tool
从外卖点单浅谈伪需求
关于 Notion-Like 工具的反思和畅想
Fuzor 2020 software installation package download and installation tutorial
国内低代码开发平台靠谱的都有哪些?
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
测试的核心价值到底是什么?
Tianyi cloud understands enterprise level data security in this way
随机推荐
国内低代码开发平台靠谱的都有哪些?
为什么 BI 软件都搞不定关联分析?带你分析分析
手把手教你处理 JS 逆向之图片伪装
Is it safe for China Galaxy Securities to open an account? Securities account opening
PG basics -- Logical Structure Management (user and permission management)
Reinforcement learning - learning notes 4 | actor critical
开源 SPL 消灭数以万计的数据库中间表
1亿单身男女撑起一个IPO,估值130亿
Startup and shutdown of CDB instances
Taishan Office Technology Lecture: from the layout height of the line, talk about the height overflow and height shrinkage of the drawing height (launched in the whole network)
CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
MMO project learning 1: preheating
数学分析_笔记_第9章:曲线积分与曲面积分
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
R语言可视化散点图(scatter plot)图、为图中的部分数据点添加标签、始终显示所有标签,即使它们有太多重叠、ggrepel包来帮忙
测试外包公司怎么样?
【AI 框架基础技术】自动求导机制 (Autograd)
Golang through pointer for Range implements the change of the value of the element in the slice
The road of enterprise digital transformation starts from here
Postman核心功能解析-参数化和测试报告