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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 魔豆

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,结束。