码迷,mamicode.com
首页 > 系统相关 > 详细

shell的while循环

时间:2017-02-25 15:40:25      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:comm   基本   文件   music   命令   inf   数据   wrap   term   

while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:

while command
do
   Statement(s) to be executed if command is true
done

命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

以下是一个基本的while循环,测试条件是:如果COUNTER小于5,那么返回 true。COUNTER从0开始,每次循环处理时,COUNTER加1。运行上述脚本,返回数字1到5,然后终止。

  1. COUNTER=0
  2. while [ $COUNTER -lt 5 ]
  3. do
  4. COUNTER=‘expr $COUNTER+1‘
  5. echo $COUNTER
  6. done

运行脚本,输出:

1
2
3
4
5


while循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按<Ctrl-D>结束循环。

  1. echo ‘type <CTRL-D> to terminate‘
  2. echo -n ‘enter your most liked film: ‘
  3. while read FILM
  4. do
  5. echo "Yeah! great film the $FILM"
  6. done

运行脚本,输出类似下面:

type <CTRL-D> to terminate
enter your most liked film: Sound of Music
Yeah! great film the Sound of Music

shell的while循环

标签:comm   基本   文件   music   命令   inf   数据   wrap   term   

原文地址:http://www.cnblogs.com/LvLoveYuForever/p/6441823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!