一、一些说明

    系统:Ubuntu12.04TLS 64位

    bash:GNU bash, 版本 4.2.25(1)-release (x86_64-pc-linux-gnu)

二、条件语句

    (1)语法:

    if 表达式    then         Commands    [elif 表达式     then        Commands    ......    else        Commands]    fi

        其中if和fi必须成对使用

     (2)例子: if.sh

     #!/bin/bash            score=80        if [ $score -ge 90 ]    then        echo "优秀"    elif [ $score -ge 70 ]    then        echo "良好"    elif [ $score -ge 60 ]    then        echo "合格"    else        echo "不合格"    fi

         $sh if.sh

        结果:“良好”

三、多路分支语句

    (1)语法:

        case 变量 in            模式1)                commands                ;;            模式2)                commands                ;; #每一中情况由两个分号结束             ......            模式n)                commands                ;;  #最后的结束符可以省略         esac

           类似与C语言的switch/case,其中每一个case是以break结束的。   

            模式可以是正则表达式,也可以是单个字符串

            正则可以使用:

                *          表示任意字符串

               ?         表示任意字符

                [a-z]    表示a到z的任意字符

                [abc]   表示a、b、c三者中的任意一个

                |          表示多重选择

    (2)例子:case.sh

    #!/bin/bash            str="mouse"        case $str in        "cat")        echo "this is a cat"        ;;                "dog" | "mouse")        echo "this is a dog"        ;;                "cattle")        echo "this is a cattle"        ;;                *)        echo "this is a $str"    esac

            "*" 表示默认情况,类似与C语言中switch/case中的default     

         $sh case.sh

         结果为: this is a dog

          

四、循环语句

    【1】for循环

           (1)语法:

        for 变量名  in 单词表        do            commands        done

            (2)例子: for.sh

        #!/bin/bash                    list=`ls`                for file in $list        do            echo "filename=$file"        done

                $sh for.sh

                结果:

                    filename=1.c

                    filename=2.c
                    filename=format
                    filename=for.sh

        

                当省略了in以及单词表时,将默认自动遍历命令行输入的所有参数

                eg.

                no_in.sh

               #!/bin/bash               echo "Number of argumens passed is $#"               for argc        #没有in, 默认遍历命令行参数               do                     echo $argc               done

                           执行结果

              >./no_in.sh a b c              >Number of argumens passed is 3               a               b               c

    【2】while循环

                    (1)语法:       

            while 命令或表达式            do                commands            done

                    (2)例子:while.sh

            #!/bin/bash                            i=5                        while [ $i -ge 0 ]            do                echo "i=$i"                i=`expr $i - 1`            done

                         $sh while.sh

                       结果:

                                i=5

                                i=4
                                i=3
                                i=2
                                i=1
                                i=0
            

      【3】until循环

                (1)语法:

           until 表达式           do               commands           done

                (2)例子:until.sh

            #!/bin/bash                            i=4                        until [ $i -lt 0 ]            do                echo "i = $i"                               i=`expr $i - 1`            done

                        $sh until.sh   

                        结果:

                            i = 4

                            i = 3
                            i = 2
                            i = 1
                            i = 0
    

五、循环控制语句

    【1】 break

       (1) break 跳出当前循环

    #!/bin/bash            i=4        while [ $i -lt 9 ]    do         echo "i=$i"        i=`expr $i + 1`        if [ $i -eq 6 ]        then            echo "i=$i, and break"            break  #当i=6时,循环将结束        fi    done

            结果:

                i=4

                i=5
                i=6, and break

        (2)break n 跳出n层循环

        

     #!/bin/bash                                 i=4     j=7     k=9                         for ((; i > 0; i-=1))     do         echo "i=$i"         for ((; j > 0; j-=1))         do             echo "j=$j"             for ((; k > 0; ))             do                 k=`expr $k - 1`                 echo "k=$k"                 if [ 0 -eq `expr $k % 2`  ]                 then                     echo "break 2"                     break 2 #将跳到最外层循环                 fi             done          done      done

            结果:

                        i=4

                        j=7
                        k=8
                        break 2
                        i=3
                        j=7
                        k=7
                        k=6
                        break 2
                        i=2
                        j=7
                        k=5
                        k=4
                        break 2
                        i=1
                        j=7
                        k=3
                        k=2
                        break 2

    【2】continue

        (1)continue 结束当前循环,转到最近一层循环,开始下一轮循环

        #!/bin/bash                i=5                while [ $i -gt 2  ]        do            i=`expr $i - 1`            if [ $i -eq 4  ]            then                echo "this is 4, and continue"                continue #i=4时,后面的echo不会执行            fi                        echo "i=$i"        done

             结果:

                    this is 4, and continue

                    i=3
                    i=2

          (2)continue n   结束当前循环,转到最近n层循环,开始下一轮循环

        #!/bin/bash        for ((i = 1; i < 3; i++))        do            echo "i=$i"            for ((j = 1; j < 3; j++))            do                echo "j=$j"                for ((k = 1; k < 3; ))                do                    echo "k=$k"                    if [ 1 -eq `expr $k % 2`  ]                    then                        echo "k=$k, continue 3"                        k=`expr $k + 1`                        continue 3   #将跳转到最外层                    fi                done            done        done

                结果:

                    i=1

                    j=1
                    k=1
                    k=1, continue 3
                    i=2
                    j=1
                    k=1
                    k=1, continue 3