• linux 简单记录4--if,for,while,case 流程控制


    --if 条件判断语句

    if 条件判断
        then 命令序列1
    else 命令序列2
    fi
    [root@iscsi opt]# vim mkmydata.sh
    #!/bin/bash
    dir="/opt/mydata"
    if [ ! -e $dir ]
    then
    mkdir -p $dir
    fi
    [root@iscsi opt]# bash mkmydata.sh 
    [root@iscsi opt]# ll /opt/mydata
    total 0
    [root@iscsi opt]# ls -d /opt/mydata
    /opt/mydata
    
    [root@iscsi opt]# vim chkhost.sh
    [root@iscsi opt]# bash chkhost.sh 10.15.7.20
    host 10.15.7.20 is on-line
    [root@iscsi opt]# bash chkhost.sh 10.15.7.21
    host 10.15.7.21 is off-line
    [root@iscsi opt]# cat chkhost.sh 
    #!/bin/bash
    ping -c 3 -i 0.2 -W 3 $1 &> /dev/null #-c 次数,-i 定义每个数据包发送间隔,-W 等待超时时间
    if [ $? -eq 0 ]
    then
    echo "host $1 is on-line"
    else
    echo "host $1 is off-line"
    fi
    # $?变量是否为0来判断上一条语句执行情况,成功为0,否则非0
    if 条件判断1
        then 命令序列1
    elif 条件判断2
        then 命令序列2
    else 命令序列3
    fi
    [root@iscsi opt]# vim chkscore.sh
    [root@iscsi opt]# bash chkscore.sh 
    Enter your score(0-100): 101
    please enter the right score :101 
    101 is Fail
    [root@iscsi opt]# bash chkscore.sh 
    Enter your score(0-100): 87
    87 is Excellent
    [root@iscsi opt]# bash chkscore.sh 
    Enter your score(0-100): 70
    70 is Pass
    [root@iscsi opt]# bash chkscore.sh 
    Enter your score(0-100): -10
    please enter the right score :-10 
    -10 is Fail
    
    [root@iscsi opt]# cat chkscore.sh 
    #!/bin/bash
    read -p "Enter your score(0-100): " GRADE
    if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ] ; then
    echo "please enter the right score :$GRADE "
    fi
    
    if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
    echo "$GRADE is Excellent"
    elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
    echo "$GRADE is Pass"
    else
    echo "$GRADE is Fail"
    fi
    
    [root@iscsi opt]# bash chkscore.sh 
    Enter your score(0-100): 101
    please enter the right score :101 
    [root@iscsi opt]# vim chkscore.sh 
    [root@iscsi opt]# bash chkscore.sh 
    Enter your score(0-100): 102
    please enter the right score :102 
    [root@iscsi opt]# cat chkscore.sh 
    #!/bin/bash
    read -p "Enter your score(0-100): " GRADE
    if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ] ; then
    echo "please enter the right score :$GRADE "
    exit 0 #exit 1
    fi
    
    if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
    echo "$GRADE is Excellent"
    elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
    echo "$GRADE is Pass"
    else
    echo "$GRADE is Fail"
    fi

    2 for循环判断语句

    for循环语句允许脚本一次性读取多个信息,然后逐一对信息进行操作处理,当要处理的数据有范围时,使用for循环很适合。
    for 变量名 in 取值列表
    do
        命令序列
    done
    [root@iscsi opt]# cat users.txt 
    yhq
    abc
    hong
    tang
    car
    [root@iscsi opt]# cat create_user.sh 
    #!/bin/bash
    read -p "Enter The Users Password : " PASSWD
    for UNAME in `cat users.txt`
    do
    id $UNAME &> /dev/null
    if [ $? -eq 0 ]
    then
    echo "Already exists"
    else
    useradd $UNAME &> /dev/null #重定向到空洞文件/dev/null
    echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null
    if [ $? -eq 0 ]
    then
    echo "$UNAME , Create success"
    else
    echo "$UNAME , Create failure"
    fi
    fi
    done
    [root@iscsi opt]# bash create_user.sh 
    Enter The Users Password : yhq
    yhq , Create success
    abc , Create success
    hong , Create success
    tang , Create success
    car , Create success
    [root@iscsi opt]# tail -n 5 /etc/passwd
    yhq:x:1004:1004::/home/yhq:/bin/bash
    abc:x:1005:1005::/home/abc:/bin/bash
    hong:x:1006:1006::/home/hong:/bin/bash
    tang:x:1007:1007::/home/tang:/bin/bash
    car:x:1008:1008::/home/car:/bin/bash
    
    使用ip地址列表,用for循环判断主机ip是否能ping通
    [root@iscsi opt]# cat iplist.txt 
    10.15.7.20
    10.15.7.21
    10.15.7.29
    [root@iscsi opt]# cat checkiplist.sh 
    #!/bin/bash
    lists=$(cat /opt/iplist.txt)
    for ip in $lists
    do
    ping -c 3 -i 0.2 -W 3 $ip &> /dev/null
    if [ $? -eq 0 ] ; then
    echo "host $ip is on-line"
    else
    echo "host $ip is off-line"
    fi
    done
    [root@iscsi opt]# bash checkiplist.sh 
    host 10.15.7.20 is on-line
    host 10.15.7.21 is off-line
    host 10.15.7.29 is off-line

    3 while条件循环

    while条件循环语句是一种让脚本根据某些条件来重复执行命令的语句,在执行前不确定最终执行的次数,不同于for循环语句中
    有目标、有范围的使用场景。
    while 条件判断操作
    do
        命令序列
    done
    [root@iscsi opt]# cat guess.sh 
    #!/bin/bash
    PRICE=$(expr $RANDOM % 1000) ##expr命令获取结果
    TIMES=0
    echo "商品实际价格为 0-999 之间,猜猜看是多少? "
    while true
    do
    read -p "请输入您猜测的价格数目: " INT
    let TIMES++ ##times变量加1
    if [ $INT -eq $PRICE ] ; then
    echo "恭喜您答对了,实际价格是 $PRICE"
    echo "您总共猜测了 $TIMES 次"
    exit 0 ##终止脚本的运行
    elif [ $INT -gt $PRICE ] ; then
    echo "太高了! "
    else
    echo "太低了! "
    fi
    done
    [root@iscsi opt]# bash guess.sh 
    商品实际价格为 0-999 之间,猜猜看是多少? 
    请输入您猜测的价格数目: 123
    太低了! 
    请输入您猜测的价格数目: 99
    太低了! 
    请输入您猜测的价格数目: 300
    太高了! 
    请输入您猜测的价格数目: 200
    太低了! 
    请输入您猜测的价格数目: 268
    太低了! 
    请输入您猜测的价格数目: 290
    太高了! 
    请输入您猜测的价格数目: 280
    恭喜您答对了,实际价格是 280
    您总共猜测了 7 次

    4 case条件判断语句

    case 变量值 in
    模式1)
        命令序列1
        ;;
    模式2)
        命令序列2
        ;;
        .....
    *)
        默认命令序列
    esac
    [root@iscsi opt]# cat checkkeys.sh 
    #!/bin/bash
    read -p "请输入一个字符,并按 Enter 键确认: " KEY
    case "$KEY" in
    [a-z]|[A-Z])
    echo "您输入的是 字母。 "
    ;;
    [0-9])
    echo "您输入的是 数字。 "
    ;;
    *)
    echo "您输入的是 空格、功能键或其他控制字符。 "
    esac
    [root@iscsi opt]# bash checkkeys.sh 
    请输入一个字符,并按 Enter 键确认: b
    您输入的是 字母。 
    [root@iscsi opt]# bash checkkeys.sh 
    请输入一个字符,并按 Enter 键确认: 2
    您输入的是 数字。 
    [root@iscsi opt]# bash checkkeys.sh 
    请输入一个字符,并按 Enter 键确认: @
    您输入的是 空格、功能键或其他控制字符。

    注:实例来自于《linux就该这么学》

  • 相关阅读:
    问题:android学习内容破碎,我个人关于如何学习android的一些个人经历
    问题:一球从某高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?
    问题:alias设置与删除
    问题:从键盘读取特定类型的数据(使用Scanner读取int类型)
    求一个数组当中最大(最小)值的两种计算方法
    Mysql5.7安装
    maven下载及安装
    Ubuntu下修改DNS重启也能用的方法
    删除CNNIC证书
    字符编码ASCII, Unicode和UTF-8概念扫盲
  • 原文地址:https://www.cnblogs.com/yhq1314/p/13164257.html
Copyright © 2020-2023  润新知