shell的while循环

来源:互联网 发布:淘宝拍立淘不能用了 编辑:程序博客网 时间:2024/05/21 07:58
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为: while commanddo   Statement(s) to be executed if command is truedone命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。 以下是一个基本的while循环,测试条件是:如果COUNTER小于5,那么返回 true。COUNTER从0开始,每次循环处理时,COUNTER加1。运行上述脚本,返回数字15,然后终止。 01.COUNTER=002.while [ $COUNTER -lt 5 ]03.do04.    COUNTER='expr $COUNTER+1'05.    echo $COUNTER06.done运行脚本,输出: 12345 while循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按<Ctrl-D>结束循环。 01.echo 'type <CTRL-D> to terminate'02.echo -n 'enter your most liked film: '03.while read FILM04.do05.    echo "Yeah! great film the $FILM"06.done运行脚本,输出类似下面: type <CTRL-D> to terminateenter your most liked film: Sound of MusicYeah! great film the Sound of Music
0 0
原创粉丝点击