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

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

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

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

#!/bin/bash

COUNTER=
while [ $COUNTER -lt ]
do
COUNTER=$(expr $COUNTER + )
echo $COUNTER
done

运行脚本,输出:


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

#!/bin/bash

echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked file: '
while read FILM
do
echo "Yeah! great file the $FILE"
done

运行结果:

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

Shell while循环的更多相关文章

  1. Linux Shell系列教程之(十二)Shell until循环

    本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...

  2. shell for循环+case的脚本(监控程序状态)

    分享一个shell for循环+case的脚本(监控程序状态) 分享一个for循环+case的脚本(监控程序状态并执行相关操作) ,供大家学习参考. 复制代码代码如下: #/bin/bash set ...

  3. shell 各种循环判断

    shell支持的循环有 Shell if else Shell case esac Shell for循环 Shell while循环 Shell until循环

  4. shell脚本--循环结构

    shell的循环结构有while和for两种 for循环 #!/bin/bash #文件名:test.sh i=4 for i in 2 4 6 8 10 do echo $i done echo $ ...

  5. Linux Shell系列教程之(十一)Shell while循环

    本文是Linux Shell系列教程的第(十一)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上一篇Linux Shell系列教程之(十)Shell for循环中,我们已经 ...

  6. Linux Shell系列教程之(十)Shell for循环

    本文是Linux Shell系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell系列教程 基本任何语言都有自己的循环语句,Shell当然也不例外,今天就为大家介绍下Shel ...

  7. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

  8. Linux shell for循环结构

    Linux Shell   for循环结构 循环结构            1:循环开始条件      2:循环操作      3:循环终止的条件 shell语言          for,while ...

  9. Shell 编程 循环语句

    本篇主要写一些shell脚本循环语句的使用. for 循环 指定次数 #!/bin/bash for ((i=1;i<=10;i++)) do echo $i done [root@localh ...

  10. Linux 之Shell for循环

    @代表所有参数所以如果后面跟上echo $v你会发现他会一次显示user userdebug eng $poo -le ${#prodlist[@]} 这句话是说 $poo小于等于prodlist中的 ...

随机推荐

  1. js获取任意元素到页面的距离

    function getPos(obj){ var pos={left:0,top:0} while(obj){ pos.left+=obj.offsetLeft; pos.top+=obj.offs ...

  2. 『奇葩问题集锦』Malformed lock file found: /var/cache/dnf/metadata_lock.pid.

    Malformed lock file found: /var/cache/dnf/metadata_lock.pid.Ensure no other dnf process is running a ...

  3. yii2单独给input或者其他标签定义class

    <?= $form->field($model, 'price5', ['options' => ['class' => 'col-sm-6']])->textInput ...

  4. Sencha Touch Guide

     1.一些命令 创建APP #  Make sure the current working directory is the Sencha Touch 2 SDK 例如D:\Program File ...

  5. 【python】for循环一列

    a="哈哈哈哈,笑死我了"for i in a: print (i, end=' ') 结果 哈 哈 哈 哈 , 笑 死 我 了

  6. 使用South时候由于两个相同id的文件引起的问题

    由于之前版本控制的一个小失误, 在主分子上面调用python manage.py makemigrations生成了 0058_auto__add_unique_setting_name_value. ...

  7. mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句

    http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases;           ...

  8. C语言-06复杂数据类型-02字符串

    #include <stdio.h> int main() { //char name[] = {'i', 't', 'c', 'H', 's', 't', '\0'}; char nam ...

  9. iOS9 升级设置

    今天升级了iOS9, Xcode7.1 ; 打开之前的工程发现网络请求出错了, 参照UM开发文档, 对info.plist进行了配置如下: 1. 以iOS9 SDK编译的工程会默认以SSL安全协议进行 ...

  10. Uva10766 Organising the Organisation

    题目链接戳这里 基尔霍夫矩阵裸题.构建基尔霍夫矩阵(度数矩阵-邻接矩阵),求他的任意\(n-1\)阶主子式的绝对值即为答案. 这题开始用java写,结果BigInteger太慢Tle了. 后来用c++ ...