当前位置:网站首页>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 !!!!
边栏推荐
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- HAC cluster modifying administrator user password
- 测试的核心价值到底是什么?
- 1亿单身男女撑起一个IPO,估值130亿
- HiEngine:可媲美本地的云原生内存数据库引擎
- 如何在2022年更明智地应用智能合约?
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- R language Visual scatter plot graph, add labels to some data points in the graph, and always display all labels, even if they have too much overlap. Ggrep package helps
- Startup and shutdown of CDB instances
- C# 语言的基本语法结构
猜你喜欢
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
自动化测试的好处
Advanced application of C # language
1亿单身男女撑起一个IPO,估值130亿
Redhat7.4 configure Yum software warehouse (rhel7.4)
How to convert word into PDF? Word to PDF simple way to share!
UDF implementation of Dameng database
Reflection and imagination on the notation like tool
Android interview, Android audio and video development
随机推荐
Hiengine: comparable to the local cloud native memory database engine
Django uses mysqlclient service to connect and write to the database
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
Blue sky drawing bed Apple quick instructions
Fundamentals of machine learning (III) -- KNN / naive Bayes / cross validation / grid search
Is it safe for China Galaxy Securities to open an account? Securities account opening
Mariadb root用户及普通用户的密码 重置
自动化测试的好处
#夏日挑战赛# HarmonyOS - 实现消息通知功能
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
Go语言学习教程(十六)
HAC cluster modifying administrator user password
R语言使用lubridate包处理日期和时间数据实战
数学分析_笔记_第9章:曲线积分与曲面积分
太牛了,看这篇足矣了
HiEngine:可媲美本地的云原生内存数据库引擎