zoukankan      html  css  js  c++  java
  • Shell教程 之流程控制

    1. if else

    1.1 if

    if语句语法格式:

    if condition
    then
        command1 
        command2
        ...
        commandN 
    fi
    

    写成一行(适用于终端命令提示符):

    if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
    

    1.2 if else

    if else 语法格式:

    if condition
    then
        command1 
        command2
        ...
        commandN
    else
        command
    fi
    

    1.3 if else-if else

    if else-if else 语法格式:

    if condition1
    then
        command1
    elif condition2 
    then 
        command2
    else
        commandN
    fi
    

    1.4 实例1

    a=10
    b=20
    if [ $a == $b ]
    then
       echo "a 等于 b"
    elif [ $a -gt $b ]
    then
       echo "a 大于 b"
    elif [ $a -lt $b ]
    then
       echo "a 小于 b"
    else
       echo "没有符合的条件"
    fi
    

    1.5 实例2

    if else语句经常与test命令结合使用,如下所示:

    num1=$[2*3]
    num2=$[1+5]
    if test $[num1] -eq $[num2]
    then
        echo '两个数字相等!'
    else
        echo '两个数字不相等!'
    fi
    

    2. for循环

    for循环一般格式为:

    for var in item1 item2 ... itemN
    do
        command1
        command2
        ...
        commandN
    done

    写成一行

    for var in item1 item2 ... itemN; do command1; command2… done;
    

    当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。

    in列表是可选的,如果不用它,for循环使用命令行的位置参数

    实例一:顺序输出当前列表中的数字:

    for loop in 1 2 3 4 5
    do
        echo "The value is: $loop"
    done

    执行结果:

    [root@test3101-3 bin]# ./test.sh 
    The value is: 1
    The value is: 2
    The value is: 3
    The value is: 4
    The value is: 5

    实例二:顺序输出字符串中的字符:

    for str in 'Uniquefu is a good man!'
    do
        echo $str
    done

    执行结果:

    [root@test3101-3 bin]# ./test.sh  
    Uniquefu is a good man!

    备注:直接输出结果,即str='Uniquefu is a good man!'

    3.While语句

    while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:

    while condition
    do
        command
    done

    实例:

    int=1
    while(( $int<=5 ))
    do
        echo $int
        let "int++"
    done

    执行结果:

    [root@test3101-3 bin]# ./test.sh 
    1
    2
    3
    4
    5
    

    while循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按<Ctrl-D>结束循环

    echo '按下 <CTRL-D> 退出'
    echo -n '输入你最喜欢的网站名: '
    while read FILM
    do

    echo "是的!$FILM 是一个好网站"
    echo -n '输入你最喜欢的网站名: '
    done

    执行结果:

    [root@test3101-3 bin]# ./test.sh  
    按下 <CTRL-D> 退出
    输入你最喜欢的网站名: 123
    是的!123 是一个好网站
    输入你最喜欢的网站名: 234
    是的!234 是一个好网站
    输入你最喜欢的网站名:

    4.无限循环

    无限循环语法格式一:

    while :
    do
        command
    done 

    格式二:

    while true
    do
        command
    done

    格式三:

    for (( ; ; ))
    

    5. until循环

    until 循环执行一系列命令直至条件为 true 时停止。

    until 循环与 while 循环在处理方式上刚好相反。

    一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。

    until 语法格式:

    until condition
    do
        command
    done
    

    condition 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。

    以下实例我们使用 until 命令来输出 0 ~ 4 的数字:

    a=0
    
    until [ ! $a -lt 5 ]
    do
       echo $a
       a=`expr $a + 1`
    done

    执行结果:

    [root@test3101-3 bin]# ./test.sh 
    0
    1
    2
    3
    4
    

    6. case

    case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case语句格式如下:

    case 值 in
    模式1)
        command1
        command2
        ...
        commandN
        ;;
    模式2)
        command1
        command2
        ...
        commandN
        ;;
    esac

    实例:

    echo '输入 1 到 4 之间的数字:'
    echo '你输入的数字为:'
    read aNum
    case $aNum in
        1)  echo '你选择了 1'
        ;;
        2)  echo '你选择了 2'
        ;;
        3)  echo '你选择了 3'
        ;;
        4)  echo '你选择了 4'
        ;;
        *)  echo '你没有输入 1 到 4 之间的数字'
        ;;
    esac

    执行结果:

    [root@test3101-3 bin]# ./test.sh  
    输入 1 到 4 之间的数字:
    你输入的数字为:
    5
    你没有输入 1 到 4 之间的数字
    

    7. break

    break命令允许跳出所有循环(终止执行后面的所有循环)。

    下面的例子中,脚本进入死循环直至用户输入数字大于5。要跳出这个循环,返回到shell提示符下,需要使用break命令。

    while :
    do
        echo -n "输入 1 到 5 之间的数字:"
        read aNum
        case $aNum in
            1|2|3|4|5) echo "你输入的数字为 $aNum!"
            ;;
            *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
                break
            ;;
        esac
    done

    执行结果:

    输入 1 到 5 之间的数字:3
    你输入的数字为 3!
    输入 1 到 5 之间的数字:7
    你输入的数字不是 1 到 5 之间的! 游戏结束
    

    8. continue

    continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环

    while :
    do
        echo -n "输入 1 到 5 之间的数字: "
        read aNum
        case $aNum in
            1|2|3|4|5) echo "你输入的数字为 $aNum!"
            ;;
            *) echo "你输入的数字不是 1 到 5 之间的!"
                continue
                echo "游戏结束"
            ;;
        esac
    done
    

    运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "游戏结束" 永远不会被执行

  • 相关阅读:
    关于Maya Viewport 2.0 API 开发的介绍视频
    春节大假
    Some tips about the life cycle of Maya thread pool
    Can I compile and run Dx11Shader for Maya 2015 on my side?
    How to get current deformed vertex positions in MoBu?
    想加入全球首届的 欧特克云加速计划吗?
    三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
    mac、window版编辑器 webstorm 2016... 永久破解方法。
    node 搭载本地代理,处理web本地开发跨域问题
    js 一维数组,转成嵌套数组
  • 原文地址:https://www.cnblogs.com/uniquefu/p/9558462.html
Copyright © 2011-2022 走看看