栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux bash shell 的结构化语句——while循环语句

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux bash shell 的结构化语句——while循环语句

结构化语句——while循环语句 1 while 的基本格式

while 的基本格式是:

while test command
do
	commands
done

其中的 test command 命令和 if-then 语句中的使用相同。如果 test command 的退出码为0,则循环继续。

zzz@ubuntu:~/my_learning$ cat test.sh 
#!/bin/bash

a=4
while [ $a -ge 0 ]
do
    echo "This value is $a"
    a=$[ $a - 1 ]
done



zzz@ubuntu:~/my_learning$ ./test.sh 
This value is 4
This value is 3
This value is 2
This value is 1
This value is 0
zzz@ubuntu:~/my_learning$ 
2. 使用多个测试命令

while 允许在while语句行定义多个测试命令。只有最后一个测试命令的状态码会被用来决定什么时候结束循环。
while 的测试命令在最后一次失败的测试命令时,也会被全部执行。
例:

while echo $val
		[ $val -ge 0 ]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880207.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号