do while和while的区别用法

博客分类:
我使用过的Linux命令之while - Bash中的While循环
本文链接:
(转载请注明出处)
while循环是Shell中常用的语法结构,它与其他编程语言中的while有些类似,只是写法有些不一样罢了。
while 条件;
格式二 死循环
while true
格式三 死循环
格式四 死循环
while [ 1 ]
格式五 死循环
while [ 0 ]
$COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
[root@jfht ~]# COUNTER=0
[root@jfht ~]# while [
$COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
The counter is 0
The counter is 1
The counter is 2
The counter is 3
The counter is 4
The counter is 5
The counter is 6
The counter is 7
The counter is 8
The counter is 9
[root@jfht ~]#
这个while循环改用for循环更好些
for ((COUNTER=0; COUNTER&10; ++COUNTER))
echo The counter is $COUNTER
[root@jfht ~]# for ((COUNTER=0; COUNTER&10; ++COUNTER))
echo The counter is $COUNTER
The counter is 0
The counter is 1
The counter is 2
The counter is 3
The counter is 4
The counter is 5
The counter is 6
The counter is 7
The counter is 8
The counter is 9
[root@jfht ~]#
while true
[root@jfht ~]# while true
2010年 10月 10日 星期日 16:35:22 CST
2010年 10月 10日 星期日 16:35:23 CST
2010年 10月 10日 星期日 16:35:24 CST
2010年 10月 10日 星期日 16:35:25 CST
2010年 10月 10日 星期日 16:35:26 CST
2010年 10月 10日 星期日 16:35:27 CSTCtrl+C
[root@jfht ~]#
示例三 读取输入
while read line
echo $line
[root@jfht ~]# while read line
echo $line
helloworld
worldCtrl+D
[root@jfht ~]#
实例四 处理命令行参数
文件 while_4.sh
echo "usage: $0 [-a] [-e &admin&] [-f &serverfile&] [-h] [-d &domain&] [-s &whois_server&] [-q] [-x &warndays&]"
while getopts ae:f:hd:s:qx: option
case "${option}" in
a) ALARM="TRUE";;
e) ADMIN=${OPTARG};;
d) DOMAIN=${OPTARG};;
f) SERVERFILE=$OPTARG;;
s) WHOIS_SERVER=$OPTARG;;
q) QUIET="TRUE";;
x) WARNDAYS=$OPTARG;;
\?) exit 1;;
echo "ALARM=$ALARM"
echo "ADMIN=$ADMIN"
[root@jfht ~]# cat while_4.sh
echo "usage: $0 [-a] [-e &admin&] [-f &serverfile&] [-h] [-d &domain&] [-s &whois_server&] [-q] [-x &warndays&]"
while getopts ae:f:hd:s:qx: option
case "${option}" in
a) ALARM="TRUE";;
e) ADMIN=${OPTARG};;
d) DOMAIN=${OPTARG};;
f) SERVERFILE=$OPTARG;;
s) WHOIS_SERVER=$OPTARG;;
q) QUIET="TRUE";;
x) WARNDAYS=$OPTARG;;
\?) exit 1;;
echo "ALARM=$ALARM"
echo "ADMIN=$ADMIN"
[root@jfht ~]# chmod +x while_4.sh
[root@jfht ~]# ./while_4.sh
[root@jfht ~]# ./while_4.sh -a
ALARM=TRUE
[root@jfht ~]# ./while_4.sh -e hy
[root@jfht ~]#
1. 为什么 while [ 0 ] 也会是死循环?
2. 怎么使用while read line从文件中读入数据?
【1】BASH Programming - Introduction HOW-TO
【2】Audbel
【3】一个有很多while示例的英文网页
浏览 86993
codingstandards
浏览: 3246048 次
来自: 上海
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
楼主咋没分析下源码呢?
tail -F 就可以吧
新手学习了,就是不明白为一个网卡配多个ip有什么用
不错,谢谢!
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'其他回答(0)
相关已解决
Powered by 英语答疑网 & ,Processed in 0.026227 second(s), 21 queries.
&加载中...503 Service Temporarily Unavailable
503 Service Temporarily Unavailable
openresty/1.11.2.4什么情况下使用while,而什么情况下又该使用do...while? - 知乎33被浏览16565分享邀请回答3添加评论分享收藏感谢收起82 条评论分享收藏感谢收起查看更多回答2 个回答被折叠()

我要回帖

更多关于 while循环 的文章

 

随机推荐