记录工作和生活点点滴滴

Linux整理笔记第十四天-while语句

bash脚本编程:

编程语言:
    数据结构

    顺序执行
    选择执行
        条件测试
            运行命令或[[ EXPRESSION ]]
                执行状态返回值;
        if
        case
    循环执行
        将某代码段重复运行多次;
        重复运行多少次?
            循环次数事先已知;
            循环次数事先未知;

            必须有进入条件和退出条件;

        for,while,until

    函数:结构化编程及代码重用;
        function

for循环语法:
    for NAME in LIST; do
        循环体
    done

    列表生成方式:
        1. 整数列表
            {start..end}
            $(seq start [[step]end])
        2. glob
            /etc/rc.d/rc3.d/K*
        3. 命令

    通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态;
        #!/bin/bash
        #
        net='172.16.250'
        uphosts=0
        downhosts=0

        for i in {1..254}; do
            ping -c 1 -w 1 ${net}.${i} &> /dev/null
            if [ $? -eq 0 ]; then
                echo "${net}.${i} is up."
                    let uphosts++
            else
                echo "${net}.${i} is down."
                    let downhosts++
            fi
        done

        echo "up hosts: $uphosts."
        echo "Down hosts:$downhosts."

while循环:
    while CONDITION; do
        循环体
    done

    CONDITION:循环控制条件:进入循环之前,先做一次判断;每一次循环之后会再次做判断;
        条件为“true”,则执行一次循环;直到条件测试为“false”终止循环;

        因此:CONDTION一般应该有循环控制变量;而此变量的值会再循环体不断的被修正;

    示例:求100以内所有正整数之和:
        #!/bin/bash
        #

        declare -i sum=0
        declare -i i=1

        while [ $i -le 100 ]; do
            let sum+=$i
            let i++
        done

        echo "$i"
        echo "summary: $sum."

练习:
1. 添加10个用户,user1-10
2. 通过ping命令探测172.16.250.1-254范围内的所有主机在线状态;(用while循环)
3. 打印九九乘法表;(分别用for和while循环事先)
4. 利用RANDOM生成10个随机数字,输出这10个数字,并显示其中最大和最小;

答案:
1. #!/bin/bash
#
declare -i i=1
declare -i users=0

    while [ $i -le 10 ]; do
        if ! id user$i &> /dev/null; then
            useradd user$i
            echo "add user:user$i."
                let users++
        fi
        let i++
    done

    echo "add $users users."

2.#!/bin/bash
  #

  declare -i i=1
  declare -i uphosts=0
  declare -i downhosts=0
  net='172.16.250'

  while [ $i -le 20 ]; do
    if ping -c 1 -w 1 $net.$i &> /dev/null; then
        echo "$net.$i is up."
        let uphosts++
    else
        echo "$net.$i is down."
        let downhosts++
    fi
    let i++

  done

  echo "up hosts: $uphosts"
  echo "down hosts:$downhosts"

  3. #!/bin/bash
    #

    for j in {1..9}; do
        for i in $(seq i $j); do
            echo -e -n "${i}X${j}=$[$i*$j]\t"
            done
            echo
    done

    #!/bin/bash
    #
    declare -i i=0
    declare -i j=0

    while [ $j -le 9 ]; do
        while [ $i -le $j ]; do
            echo -e -n "${i}X${j}=$[$i*$j]\t"
            let i++
        done
        echo
        let i=1
        let j++
    done

    4.  #!/bin/bash
        #
        declare -i max=0
        declare -i min=0
        declare -i i=1

        rand=$RANDOM
        echo $rand
        max=$rand
        min=$rand

        while [ $i -le 9 ]; do
            rand=$RANDOM
            echo $rand
            if [ $rand -gt $max ]; then
                max=$rand
            fi
            if [ $rand -lt $min ]; then
                min=$rand
            fi
            let i++
        done

        echo "MAX:$max"
        echo "MIN:$min"
未经允许不得转载:许春刚技术博客 » Linux整理笔记第十四天-while语句

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址