美文网首页
流程控制_for循环与select

流程控制_for循环与select

作者: 慕知 | 来源:发表于2020-12-28 15:19 被阅读0次

    一,语法

    1,shell风格的for循环

    fou循环 又叫取值循环
    语法:
    for x in 参数值1 参数值2 参数值3
    do
    ...
    done
    
    例1):
    [root@m01~]# vim /script/for1.sh
    #!/bin/bash
    for i in 111 222 "abx"
    do
        echo "hello"
    done
    
    执行:
    [root@m01~]# chmod +x /script/for1.sh
    [root@m01~]# /script/for1.sh
    hello
    hello
    hello
    
    
    例2):加上了参数
    [root@m01~]# vim /script/for1.sh
    #!/bin/bash
    for i in 111 222 "abx"
    do
        echo "hello $i" 
    done
    
    
    [root@m01~]# /script/for1.sh
    hello 111
    hello 222
    hello abx
    
    
    
    例3):
    [root@m01~]# vim /script/for1.sh
    #!/bin/bash
    for i in `ls /root`
    do
        echo "hello $i"
    done
    
    
    
    [root@m01~]# /script/for1.sh
    hello 2
    hello 3
    hello 4
    hello 8
    hello ab
    
    
    
    例4):
    [root@m01~]# vim /script/for1.sh
    #!/bin/bash
    for i in {1..4}
    do
        echo "hello $i"
    done
    echo =====
    for i in {a,c,z}
    do
        echo "hello $i"
    done
    
    
    
    
    [root@m01~]# /script/for1.sh
    hello 1
    hello 2
    hello 3
    hello 4
    =====
    hello a
    hello c
    hello z
    

    2,C风格的for循环

    [root@m01~]# vim /script/for2.sh
    #!/bin/bash
    for ((i=1;i<4;i++))
    do
        echo $i
    done
    
    
    
    [root@m01~]# chmod +x /script/for2.sh
    [root@m01~]# /script/for2.sh
    1
    2
    3
    
    
    ##  i=1是初始值,i<4是条件,i++是语法无实际意义
    

    3,continue和break

    1) continue
    [root@m01~]# vim /script/for3.sh
    #!/bin/bash
    for i in {1..5}
    do
        if [ $i -eq 3 ];then
            continue
        fi
        echo $i
    done
    
    
    
    [root@m01~]# chmod +x /script/for3.sh
    [root@m01~]# /script/for3.sh
    1
    2
    4
    5
    
    
    2) break
    [root@m01~]# vim /script/for3.sh
    #!/bin/bash
    for i in {1..5}
    do
        if [ $i -eq 3 ];then
            break
        fi
        echo $i
    done
    
    
    
    [root@m01~]# /script/for3.sh
    1
    2
    
    

    二,案例

    1, ping 网通

    [root@m01~]# vim /script/for4.sh
    #!/bin/bash
    for i in {1..100}
    do
        (ping -c 10.0.0.$i &> /dev/null
        if [ $? -eq 0 ];then
            echo "10.0.0.$i is up"
        else
            echo "10.0.0.$i is down"
        fi) &
    done
    
    
    [root@m01~]# 10.0.0.7 is down
    10.0.0.10 is down
    10.0.0.5 is down
    10.0.0.6 is down
    10.0.0.1 is down
    10.0.0.2 is down
    10.0.0.3 is down
    10.0.0.8 is down
    10.0.0.9 is down
    10.0.0.4 is down
    

    2,计算文件夹里各类文件数量

    [root@m01~]# vim /script/for5.sh
    #!/bin/bash
    dir="/etc"
    file=0
    link=0
    directory=0
    others=0
    for i in `ls $dir`
    do
        if [ -f "$dir/$i" ];then
            let file++
        elif [ -L "$dir/$i" ];then
            let link++
        elif [ -d "$dir/$i" ];then
            let directory++
        else
            let others++
        fi
    done
    
    echo "普通文件数量为:$file"
    echo "软连接文件数量为:$link"
    echo "目录文件数量为:$directory"
    echo "其他文件数量为:$others"
    
    
    [root@m01~]# /script/for5.sh
    普通文件数量为:109
    软连接文件数量为:8
    目录文件数量为:83
    其他文件数量为:0
    
    
    ##注意,字符一定要加上双引号
    

    三,select

    用于交互式环境
    1)
    [root@m01~]# vim /script/select.sh
    #!/bin/bash
    PS3="输入编号:"
    select cho in {"退出","取款","转账","查询余额"}
    do
        case $cho in
        "退出")
            break
            ;;
        "取款")
            echo "取款中"
            ;;
        "转账")
            echo "转账中"
            ;;
        "查询余额")
            echo "查询余额中"
            ;;
        esac
    done
    
    
    
    [root@m01~]# /script/select.sh
    1) 退出
    2) 取款
    3) 转账
    4) 查询余额
    输入编号:2
    取款中
    输入编号:1
    
    
    
    
    
    
    2)
    [root@m01~]# vim /script/select.sh
    #!/bin/bash
    PS3="输入编号:"
    select cho in
    do
        case $cho in
        "退出")
            break
            ;;
        "取款")
            echo "取款中"
            ;;
        "转账")
            echo "转账中"
            ;;
        "查询余额")
            echo "查询余额中"
            ;;
        esac
    done
    
    
    
    [root@m01~]# /script/select.sh 退出 取款 转账 查询余额
    1) 退出
    2) 取款
    3) 转账
    4) 查询余额
    输入编号:3
    转账中
    输入编号:1
    

    相关文章

      网友评论

          本文标题:流程控制_for循环与select

          本文链接:https://www.haomeiwen.com/subject/fhhonktx.html