惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

博客园 - 魔豆

oracle中使用unpivot实现列转行 推荐一个可以下载B站视频的工具 学习qt,做了一个小应用:随机点名提问系统 hadoop的eclipse插件 mysql5.5安装到最后一步卡死无响应的解决方法 Hbase各版本支持情况 Comparable接口的使用 css3实现动画效果 HTML5中使用EventSource实现服务器发送事件 HTML5中的Web Worker技术 css3中的盒子模型 使用visual studio code运行html 【转】解决chrome浏览器不支持audio和video标签的autoplay自动播放 spring mvc中添加对Thymeleaf的支持 Eclipse安装代码反编译插件Enhanced Class Decompiler 使用idea创建web项目 shell编程学习笔记(十二):Shell中的break/continue跳出循环 shell编程学习笔记(十):Shell中的for循环 shell编程学习笔记(九):Shell中的case条件判断
shell编程学习笔记(十一):Shell中的while/until循环
魔豆 · 2019-03-17 · via 博客园 - 魔豆

shell中也可以实现类似java的while循环

while循环是指满足条件时,进行循环

示例:

1 #! /bin/sh
2 index=10
3 while [ $index -gt 0 ]
4 do
5 index=$((index-1));
6 echo $index
7 done

while循环以whille开始,循环体以do开始,以done结束

注意第5行的代码,表达式index-1外面添加了$(()),如果不添加$(())的话,会报错,因为这里index是字符串,得到的结果不是9,而是10-1

第5行的index-1也可以写成--index,这个跟java语言一致。

我把上面的代码稍做修改:

#! /bin/sh
index=0
while [ $index -gt 0 ]
do
index=$((index-1));
echo $index
done

执行这段代码并不会输入任何内容,说明必须满足条件才会执行,不存在循环第1条时必定会执行的情况

为了运算index-1,上面使用了$(()),不然只会当字符串来处理,当然了,可以使用declare -i index=10直接把index声明为整体:

#! /bin/sh
declare -i index=10
while [ $index -gt 0 ]
do
index=index-1;
echo $index
done

declare的参数声明:

  • +/-  "-"可用来指定变量的属性,"+"则是取消变量所设的属性。
  • -f  仅显示函数。
  • r  将变量设置为只读。
  • x  指定的变量会成为环境变量,可供shell以外的程序来使用。
  • i  [设置值]可以是数值,字符串或运算式。

until循环刚好跟while循环相反,是指不满足条件时,进行循环

示例:

#! /bin/sh
declare -i index=10
until [ $index -lt 0 ]
do
echo $index
index=index-1;
done

以上示例执行时,会从10开始循环输出,输出到0,结束。