• shell循环语法


    while do done, until do done (不定回圈)

    一般来说,不定回圈最常见的就是底下这两种状态了:

    while [ condition ]  <==中括号内的状态就是判断式
    do            <==do 是回圈的开始!
        程序段落
    done          <==done 是回圈的结束

    while 的中文是『当....时』,所以,这种方式说的是『当 condition 条件成立时,就进行回圈,直到 condition 的条件不成立才停止』的意思。

     

    还有另外一种不定回圈的方式:

    until [ condition ]
    do
        程序段落
    done

    这种方式恰恰与 while 相反,它说的是『当 condition 条件成立时,就终止回圈, 否则就持续进行回圈的程序段。』是否刚好相反啊

     

    我们以 while 来做个简单的练习好了。 假设我要让使用者输入 yes 或者是 YES 才结束程序的运行,否则就一直进行告知使用者输入字串。

    [root@www scripts]# vi sh13.sh
    #!/bin/bash
    # Program:
    #    Repeat question until user input correct answer.
    # History:
    # 2005/08/29    VBird    First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    while [ "$yn" != "yes" -a "$yn" != "YES" ]
    do
        read -p "Please input yes/YES to stop this program: " yn
    done
    echo "OK! you input the correct answer."

    上面这个例题的说明是『当 $yn 这个变量不是 "yes" 且 $yn 也不是 "YES" 时,才进行回圈内的程序。』 而如果 $yn 是 "yes" 或 "YES" 时,就会离开回圈罗~那如果使用 until 呢?呵呵有趣罗~ 他的条件会变成这样:

    [root@www scripts]# vi sh13-2.sh
    #!/bin/bash
    # Program:
    #    Repeat question until user input correct answer.
    # History:
    # 2005/08/29    VBird    First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    until [ "$yn" == "yes" -o "$yn" == "YES" ]
    do
        read -p "Please input yes/YES to stop this program: " yn
    done
    echo "OK! you input the correct answer."

    仔细比对一下这两个东西有啥不同喔! ^_^再来,如果我想要计算 1+2+3+....+100 这个数据呢? 利用回圈啊~他是这样的:

    [root@www scripts]# vi sh14.sh
    #!/bin/bash
    # Program:
    #    Use loop to calculate "1+2+3+...+100" result.
    # History:
    # 2005/08/29    VBird    First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    s=0  # 这是加总的数值变量
    i=0  # 这是累计的数值,亦即是 1, 2, 3....
    while [ "$i" != "100" ]
    do
        i=$(($i+1))   # 每次 i 都会添加 1 
        s=$(($s+$i))  # 每次都会加总一次!
    done
    echo "The result of '1+2+3+...+100' is ==> $s"

    for...do...done(固定循环)

    他的语法是:

    for var in con1 con2 con3 ...
    do
        程序段
    done

    以上面的例子来说,这个 $var 的变量内容在回圈工作时:

    1. 第一次回圈时, $var 的内容为 con1 ;
    2. 第二次回圈时, $var 的内容为 con2 ;
    3. 第三次回圈时, $var 的内容为 con3 ;
    4. ....

    我们可以做个简单的练习。假设我有三种动物,分别是 dog, cat, elephant 三种, 我想每一行都输出这样:『There are dogs...』之类的字样,则可以:

    [root@www scripts]# vi sh15.sh
    #!/bin/bash
    # Program:
    #     Using for .... loop to print 3 animals
    # History:
    # 2005/08/29    VBird    First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    for animal in dog cat elephant
    do
        echo "There are ${animal}s.... "
    done

    我想要侦测的网域是本机所在的 192.168.1.1~192.168.1.100,由於有 100 台主机, 总不会要我在 for 后面输入 1 到 100 吧?此时你可以这样做喔!

    [root@www scripts]# vi sh17.sh
    #!/bin/bash
    # Program
    #       Use ping command to check the network's PC state.
    # History
    # 2009/02/18    VBird   first release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    network="192.168.1"              # 先定义一个网域的前面部分!
    for sitenu in $(seq 1 100)       # seq 为 sequence(连续) 的缩写之意
    do
        # 底下的程序在取得 ping 的回传值是正确的还是失败的!
            ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
        # 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN)
            if [ "$result" == 0 ]; then
                    echo "Server ${network}.${sitenu} is UP."
            else
                    echo "Server ${network}.${sitenu} is DOWN."
            fi
    done

    我想要让使用者输入某个目录档名, 然后我找出某目录内的档名的权限,该如何是好?

    [root@www scripts]# vi sh18.sh
    #!/bin/bash
    # Program:
    #    User input dir name, I find the permission of files.
    # History:
    # 2005/08/29    VBird    First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    # 1. 先看看这个目录是否存在啊?
    read -p "Please input a directory: " dir
    if [ "$dir" == "" -o ! -d "$dir" ]; then
        echo "The $dir is NOT exist in your system."
        exit 1
    fi
    
    # 2. 开始测试文件罗~
    filelist=$(ls $dir)        # 列出所有在该目录下的文件名称
    for filename in $filelist
    do
        perm=""
        test -r "$dir/$filename" && perm="$perm readable"
        test -w "$dir/$filename" && perm="$perm writable"
        test -x "$dir/$filename" && perm="$perm executable"
        echo "The file $dir/$filename's permission is $perm "
    done

    for...do...done 的数值处理

    除了上述的方法之外,for 回圈还有另外一种写法!语法如下:

    for (( 初始值; 限制值; 运行步阶 ))
    do
        程序段
    done

    这种语法适合於数值方式的运算当中,在 for 后面的括号内的三串内容意义为:

    • 初始值:某个变量在回圈当中的起始值,直接以类似 i=1 配置好;
    • 限制值:当变量的值在这个限制值的范围内,就继续进行回圈。例如 i<=100;
    • 运行步阶:每作一次回圈时,变量的变化量。例如 i=i+1。

    值得注意的是,在『运行步阶』的配置上,如果每次添加 1 ,则可以使用类似『i++』的方式,亦即是 i 每次回圈都会添加一的意思。好,我们以这种方式来进行 1 累加到使用者输入的回圈吧!

    [root@www scripts]# vi sh19.sh
    #!/bin/bash
    # Program:
    #     Try do calculate 1+2+....+${your_input}
    # History:
    # 2005/08/29    VBird    First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    read -p "Please input a number, I will count for 1+2+...+your_input: " nu
    
    s=0
    for (( i=1; i<=$nu; i=i+1 ))
    do
        s=$(($s+$i))
    done
    echo "The result of '1+2+3+...+$nu' is ==> $s"

    转自 http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_5.php#loop

  • 相关阅读:
    使用Lazy对构造进行重构后比较
    Ninject Lazy Load
    在 MVC 中使用 ninject Lazy Load的一个想法
    在Ninject 向构造参数中注入具有相同类型的参数
    关于 SimpleMembership 中 CreateDate 的问题
    ubuntu下谷歌浏览器字体模糊解决方案
    ubuntu双系统时间错乱
    WPS for Linux字体配置(Ubuntu 16.04)
    VS常见错误
    VMware虚拟机ubuntu显示屏幕太小解决办法
  • 原文地址:https://www.cnblogs.com/ggjucheng/p/2748633.html
Copyright © 2020-2023  润新知