bash循环for/while/until

shell流程控制之一:for循环
    for VAR in LIST; do
        STATEMENT1
        ...
    done
   
    例:
        for i in {0..5}; do
            echo $i
        done
   
    循环次数: 为列表中的元素的个数
    LIST: 列表,包含至少一个元素的字符串集合
        1) 直接给出
            例:
                for  i in lian shu; do
                    echo $1
                done
                    第一次打印lian,第二次打印shu
        2) 数值列表:{start..end}, 例: {1..10}
                `seq LAST`
                `seq FIRST LAST`
                `seq FIRST STEP LAST`
                例:    for i in `seq 5`; do
                        echo $i
                    done
                        依次打印数字1-5
                   
                    for i in `seq 5 10`; do
                        echo $i
                    done
                        依次打印数字5-10
                   
                    for i in `seq 5 2 10`; do
                        echo $i
                    done
                        依次打印数字5,7,9
        3) 返回列表的命令
            例:
                #!/bin/bash
                for i in `cat /etc/passwd` ;do
                    echo $i
                done
               
                #!/bin/bash
                for i in `ls /etc/` ;do
                    echo $i
                done
               
        4) globbing
            例:
                #!/bin/bash
                for i in /tmp/aa* ;do
                    echo $i
                done
                    文件名通配
                   
        5) 变量引用
            $*: 文件的参数列表
            $@:文件的参数个数
           
            例:
                #!/bin/bash
                for i in $* ;do
                    echo $i
                done
                    文件名通配
       
   
    睡眠: sleep #: #代表数字
   
        例如:添加3个用户,user1, user2, user3; 密码同用户名;
            #!/bin/bash
           
            for i in {1..3}; do
                if useradd user$i &> /dev/null ; then
                    echo "user$i" | passwd --stdin user$i
                    echo "Success to add user$i"
                else
                    echo "Fail to add user$i"
                fi
            done
           
        练习1:于/tmp/test目录中创建10个空文件f1,.., f10;
            #!/bin/bash
            for i in {1..10} ;do
                touch f$i
            done
           
        练习2:求100以内所有正整数之和;
            #!/bin/bash
            let sum=0
            for i in {1..100} ;do
                let sum=sum+$1
            done
            echo "sum=$sum"
        练习3:求100以内所有偶数之和;以及所有奇数之和;
        练习4:计算当前系统上所有用户ID之和;
        练习5:显示当前系统所有默认shell为bash的用户的总数;并统计此些用户ID之和;

        练习:写一个脚本
            使用ping命令探测192.168.0.X主机的在线状态
            ping -w 2 192.168.0.2
                -w #:是尝试ping几次,之后会结束,否则会一直ping
            X在此处代表的是1-254
           
            #!/bin/bash
            for i in {1..254}; do
                if ping -w 2 192.168.0.$i &> /dev/null ;then
                    echo "192.168.0.$i is online."
                else
                    echo "192.168.0.$i is offline."
                fi
            done
   
    for几种特殊情况
        1) for省略,会自动获取脚本参数列表
        2) C编程风格
            for ((变量赋值;循环条件;修正表达式)); do
                CMD1
                CMD2
            done
        3)循环嵌套
            for i in [LIST1];do
                CMD1
                for j in [LIST2];do
                    CMD2
                done
            done
        练习:写一个脚本    X  0-255   Y 1-254
            (1) ping 172.16.X.Y内的所有主机;
                172.16.0-255.1-254

            for i in {0..255}; do
                for j in {1..254}; do
                    ping -c1 -w1 172.16.$i.$j
                done
            done

        练习2:写个脚本
            (1) 传递一些目录给脚本;
            (2) 逐个显示每个目录下的所有一级文件的内容类型;
            (3) 统计一个有多少个目录;一共显示了多少个文件;

            #!/bin/bash
            #
            declare -i dirs=0
            declare -i files=0

            for d in $*; do
                for f in $d/*; do
                    file $f
                    let files++
                done
                let dirs++
            done

            echo "Directories: $dirs."
            echo "Files: $files."       
   
    bash中产生伪随机数:$RANDOM
        0-32767
       
        练习1:生成10个随机数,返回其最大值
            #!/bin/bash
            declare -i max=0
           
            for i in {1..10};do
                random=$RANDOM
                LIST="$LIST $random"
                if [ $random -ge $max ];then
                    max=$random
                fi
            done
           
            echo "List Number:$LIST"
            echo " Max Number:$max"
           
        练习1:使用for循环,打印九九乘法表
                #!/bin/bash
                #
                for i in {1..9}; do
                    for j in `seq 1 $i`; do
                        echo -n -e "${j}x${i}=$[$i*$j]\t"
                    done
                    echo
                done

        练习2:写一个脚本,判断给定的用户是否登录了当前系统;
            (1) 如果登录了,则脚本终止;
            (2) 每5秒种,查看一次用户是否登录;

while循环和函数
    while CONDITION; do
        循环体
        循环控制变量的修正表达式
    done
   
    当CONDITON为真,进入循环体,直到假时退出

    练习1:计算100以内所有偶数之和
        #!/bin/bash
        declare -i i=1
        declare -i evensum=0
       
        while [ $i -le 100 ];do
            if [ $[$i%2] -eq 0 ];do
                let evensum+=$i
            fi
            let i++
        done
        echo "evensum=$evensum"
   
until循环:
    until CONDITION;do
        循环体
        循环控制变量修正表达式
    done
   
    当CONDITON为假时进入循环,为真时退出
   
    练习1:计算100以内所有偶数之和
        #!/bin/bash
        declare -i i=1
        declare -i evensum=0
       
        until [ $i -gt 100 ];do
            if [ $[$i%2] -eq 0 ];do
                let evensum+=$i
            fi
            let i++
        done
        echo "evensum=$evensum"
   
    练习2:分别使用while和until循环实现添加10个用户:myuser1-myuser10
            #!/bin/bash
            #
            declare -i i=1
            declare -i j=0

            until [ $i -gt 10 ]; do
                if ! id myuser$i &> /dev/null; then
                useradd myuser$i
                    let j++
                fi
                let i++
            done

            echo "Add users [total]: $j."   
           
    练习3:打印九九乘法表,要求内外层循环分别使用while或until;
    练习4:打印倒序的九九乘法表,要求内外层循环分别使用while或until;


    循环控制命令:
        break: 提前退出循环
            break N: 退出N层循环,N省略时表示退出break语句所在的循环
        continue: 提前结束本轮循环,而直接进入下轮循环
            continue N: 提前结束第N层循环的本轮循环,而直接进入下轮循环
       
        while CONDITION; do
            CMD1
            if CONDITION2; then
                break [N]
            fi
            CMD2
            ...
        done
       
        死循环:
            while true;do
                循环体
            done
           
            until false; do
                循环体
            done
           
            练习2:写一个脚本,判断给定的用户是否登录了当前系统;
            (1) 如果登录了,则脚本终止;
            (2) 每5秒种,查看一次用户是否登录;

            方法一:
            #!/bin/bash
            #
            declare -i status=0

            #显示当年有哪些用户登录
            who | grep "centos" &> /dev/null
            status=$?
           

            until [ $status -eq 0 ]; do
                sleep 5
                who | grep "centos" &> /dev/null
                status=$?
            done

            echo "centos is logged on."       

            方法二
            #!/bin/bash
            #
            declare -i status=0

            until who | grep "centos" &> /dev/null; do
                sleep 5
            done

            echo "centos is logged on."

            方法三
            #!/bin/bash
            #
            while true; do
                who | grep "centos" &> /dev/null
                if [ $? -eq 0 ];then
                break
                fi
                sleep 5
            done

            echo "centos is logged."

while的特殊用法:遍历指定文件的每一行
    while read line; do
        循环体
    done <文件路径

    练习:找出其UID为偶数的所有用户的用户名;并显示其ID号;

        #!/bin/bash
        #
        file=/etc/passwd

        while read line; do
            userid=`echo $line | cut -d: -f3`
            if [ $[$userid%2] -eq 0 ]; then
                echo $line | cut -d: -f1,3
            fi
        done < $file   
           
    练习:输出给定的文件的偶数行的行号,以及其行内信息统统改为大写字母输出;

        declare -i i=1
        while read line; do
            if [ $[$i%2] -eq 0 ]; then
                echo -n "$i "
                echo $line | tr 'a-z' 'A-Z'
            fi
            let i++
        done < /path/from/somefile

    练习:显示如下菜单给用户
        cpu) cpuifno
        mem) memory infomation
        disk) disk infomation
        quit) quit

        (1) 用户给定的选项后,显示相应的信息;而后提示用户再次选择;
        (2) 非正确选择也提示用户重新选择,并说明,如果想退出请输入"quit";

        #!/bin/bash
        #
        while true; do
        cat << EOF
        cpu) cpu
        mem) memory
        disk) disk
        quit) quit
        EOF

            read -p "Your choice: " choice
            case $choice in
            cpu)
                lscpu ;;
            mem)
                free ;;
            disk)
                fdisk -l /dev/sd[a-z] ;;
            quit)
                break ;;
            esac
        done

posted @ 2015-10-26 10:37  lucky_zhang  阅读(892)  评论(0编辑  收藏  举报