当前位置:网站首页>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 !!!!
边栏推荐
- 1亿单身男女撑起一个IPO,估值130亿
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- 如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
- Get wechat avatar and nickname with uniapp
- Ultrasonic ranging based on FPGA
- vagrant2.2.6支持virtualbox6.1版本
- Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
- 开源 SPL 消灭数以万计的数据库中间表
- 测试的核心价值到底是什么?
- 手把手教你处理 JS 逆向之图片伪装
猜你喜欢

如何实现游戏中的在线计时器和离线计时器

IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系

JAD的安装、配置及集成IDEA

2022 the latest big company Android interview real problem analysis, Android development will be able to technology

cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】

cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】

测试外包公司怎么样?

The basic grammatical structure of C language

Summer Challenge database Xueba notes, quick review of exams / interviews~

Password reset of MariaDB root user and ordinary user
随机推荐
Postman核心功能解析-参数化和测试报告
泰山OFFICE技术讲座:由行的布局高度,谈绘制高度的高度溢出、高度缩水(全网首发)
Technology sharing | common interface protocol analysis
手把手教你处理 JS 逆向之图片伪装
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
Mysql database indexing tutorial (super detailed)
Summer Challenge database Xueba notes, quick review of exams / interviews~
Fuzor 2020软件安装包下载及安装教程
How to realize the Online timer and offline timer in the game
Hiengine: comparable to the local cloud native memory database engine
块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
flume系列之:拦截器过滤数据
PG basics -- Logical Structure Management (user and permission management)
PG基础篇--逻辑结构管理(用户及权限管理)
The road of enterprise digital transformation starts from here
Reflection and imagination on the notation like tool
[performance test] jmeter+grafana+influxdb deployment practice
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
What are the reliable domestic low code development platforms?
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL