当前位置:网站首页>shell编程基础(第9篇:循环)
shell编程基础(第9篇:循环)
2022-07-05 18:58:00 【叫我王员外就行】
前言
在生活中我们总会做重复的事情,程序中使用循环模拟做重复的事情,任何语言都有循环语句,在shell编程中,支持3种循环的语法,分别是while、for、for in!
while的两种语法
1、放在同一行
while condition; do
command
donewhile与do放在同一行,使用;隔开,while每次会判断condition的退出状态码,如果为0,则执行循环体的代码
2、不放在同一行
while condition
do
command
donewhile与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
donefor 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、无论什么语言,遵守其标准语法,学习起来事半功倍!!!!
边栏推荐
- 华为让出的高端市场,小米12S靠徕卡能抢到吗?
- Ant group open source trusted privacy computing framework "argot": open and universal
- 跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
- XML基础知识概念
- Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- 决策树与随机森林
- 5. Data access - entityframework integration
- Windows Oracle open remote connection Windows Server Oracle open remote connection
- Go语言学习教程(十六)
猜你喜欢

Technology sharing | common interface protocol analysis
![[detailed explanation of AUTOSAR 14 startup process]](/img/c4/5b08b43db97d8bb10c7207005f8c0a.png)
[detailed explanation of AUTOSAR 14 startup process]
![[performance test] jmeter+grafana+influxdb deployment practice](/img/32/f07792734d040829398a90a2040146.png)
[performance test] jmeter+grafana+influxdb deployment practice

HiEngine:可媲美本地的云原生内存数据库引擎
Django使用mysqlclient服务连接并写入数据库的操作过程

JAD installation, configuration and integration idea

JAD的安装、配置及集成IDEA

Word查找红色文字 Word查找颜色字体 Word查找突出格式文本
Mysql如何对json数据进行查询及修改

Summary of six points of MySQL optimization
随机推荐
Decision tree and random forest
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
China law network joins hands to observe the cloud, and the online system is a full link observable platform
Startup and shutdown of CDB instances
HAC集群修改管理员用户密码
android中常见的面试题,2022金九银十Android大厂面试题来袭
The monthly list of Tencent cloud developer community videos was released in May 2022
Oracle 中文排序 Oracle 中文字段排序
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
UDF implementation of Dameng database
一朵云开启智慧交通新未来
Optimization of middle alignment of loading style of device player in easycvr electronic map
IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
Go语言 | 02 for循环及常用函数的使用
Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
R语言可视化散点图(scatter plot)图、为图中的部分数据点添加标签、始终显示所有标签,即使它们有太多重叠、ggrepel包来帮忙
AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources
Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]