当前位置:网站首页>shell编程基础(第9篇:循环)
shell编程基础(第9篇:循环)
2022-07-05 18:58:00 【叫我王员外就行】
前言
在生活中我们总会做重复的事情,程序中使用循环模拟做重复的事情,任何语言都有循环语句,在shell编程中,支持3种循环的语法,分别是while、for、for in!
while的两种语法
1、放在同一行
while condition; do
command
done
while与do放在同一行,使用;隔开,while每次会判断condition的退出状态码,如果为0,则执行循环体的代码
2、不放在同一行
while condition
do
command
done
while与do不在同一行,此时不需要;
单行写法
while true; do echo 'hello world'; done
使用;隔开即可
只判断退出状态码
while true; do
echo 'hello world';
done
由于true这个命令的退出状态码永远为0,所以循环将一直进行下去。只要有退出状态码的命令都可以放在while的后面,它们可以是
1、函数
2、一个命令
3、另一个脚本程序
等等,只要有退出状态码,则可作为条件
多个条件,只认最后一个条件
while true; false; do
echo 'hello world';
done
同一行,只认最后一个条件的退出状态码
while总结
1、几乎与if的标准一样,条件就是退出状态码,所以test命令也经常作为条件来使用
2、while的使用率还挺多的,请学会它吧
for in语法
1、放在同一行
for item in hello world; do
echo $item
done
2、不放在同一行
for item in hello world
do
echo $item
done
列表
在for in语法中,in后面跟着是列表,且列表以空白字符(空格、换行、制表、任意字符)分隔的单词组成的列表,循环中,列表中的单词会一个一个被遍历,所以我们经常可以利用命令替换的返回值,进行遍历,比如遍历当前工作目录下的每一个文件
for file in $(ls);do
echo $file
done
写在一行
for item in hello world;do echo $item; done
利用文件名替换
for i in *.png; do
echo $i
done
for in总结
1、记住一条,in后面的是由空白字符分隔的单词列表(注意:IFS可以修改默认的分隔符)
for语法
类C语言的创新是计算机高级语言的鼻祖,shell编程同样也学习C语言的for语法
for (( i=0; i<5; i=i+1 )); do
echo $i
done
语法呢,双括号,哈哈,躲不掉!!在双圆括号中,访问变量值,连美元符都可以省去了!!
无限循环
for ((;;))
do
echo hello world
sleep 5
done
最后的总结
1、shell编程中,支持while、for in、for,3种循环方式
2、其实还有另外一种until,由于不常用,本文不再提及
3、无论什么语言,遵守其标准语法,学习起来事半功倍!!!!
边栏推荐
- #夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
- cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
- EasyCVR电子地图中设备播放器loading样式的居中对齐优化
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- 【AI 框架基础技术】自动求导机制 (Autograd)
- 2022最新大厂Android面试真题解析,Android开发必会技术
- Precautions for RTD temperature measurement of max31865 module
- 5. Data access - entityframework integration
- Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
- Fuzor 2020軟件安裝包下載及安裝教程
猜你喜欢
Technology sharing | common interface protocol analysis
What are the reliable domestic low code development platforms?
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
Shang Silicon Valley Shang preferred project tutorial release
Mysql如何对json数据进行查询及修改
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
Applet modification style (placeholder, checkbox style)
ELK分布式日志分析系统部署(华为云)
Go deep into the underlying C source code and explain the core design principles of redis
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
随机推荐
A cloud opens a new future of smart transportation
The monthly list of Tencent cloud developer community videos was released in May 2022
Optimization of middle alignment of loading style of device player in easycvr electronic map
AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources
为什么 BI 软件都搞不定关联分析?带你分析分析
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
2022 the latest big company Android interview real problem analysis, Android development will be able to technology
Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
Go语言 | 02 for循环及常用函数的使用
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
Go语言学习教程(十五)
手机开户选择哪家券商公司比较好哪家平台更安全
Fuzor 2020軟件安裝包下載及安裝教程
flume系列之:拦截器过滤数据
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
Ant group open source trusted privacy computing framework "argot": open and universal
1亿单身男女撑起一个IPO,估值130亿
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
android中常见的面试题,2022金九银十Android大厂面试题来袭