• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            linux shell 流程控制(轉)

            (peakflys注:轉載自:http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html)

            linux shell有一套自己的流程控制語句,其中包括條件語句(if),循環語句(for,while),選擇語句(case)。下面我將通過例子介紹下,各個語句使用方法。

             

            一、shell條件語句(if用法)

            if語句結構[if/then/elif/else/fi]

             

            if 條件測試語句

            then

            action

            [elif 條件

            action

            else

            action

            ]

            fi

             

            如果對于:條件測試語句不是很清楚,可以參考:linux shell 邏輯運算符、邏輯表達式詳解

            shell命令,可以按照分號分割,也可以按照換行符分割。如果想一行寫入多個命令,可以通過“';”分割。

            如:

            [chengmo@centos5 ~]$ a=5;if [[ a -gt 4 ]] ;then echo 'ok';fi;                        
            ok

             

            實例:(test.sh)

            #!/bin/sh

            scores=40;
            if [[ $scores -gt 90 ]]; then
                echo "very good!";
            elif [[ $scores -gt 80 ]]; then
                echo "good!";
            elif [[ $scores -gt 60 ]]; then
                echo "pass!";
            else
                echo "no pass!";
            fi;

             

            image

            條件測試有:[[]],[],test 這幾種,注意:[[]] 與變量之間用空格分開。

             

            二、循環語句(for,while,until用法):

            • for循環使用方法(for/do/done)

            語法結構:

             1.for … in 語句

            for 變量 in seq字符串

            do

            action

            done

            說明:seq字符串 只要用空格字符分割,每次for…in 讀取時候,就會按順序將讀到值,給前面的變量。

            實例(testfor.sh):

            #!/bin/sh

            for i in $(seq 10); do
                echo $i;
            done;

            image

            seq 10 產生 1 2 3 。。。。10空格分隔字符串。

            2.for((賦值;條件;運算語句))

            for((賦值;條件;運算語句))

            do

            action

            done;

            實例(testfor2.sh):

            #!/bin/sh

            for((i=1;i<=10;i++));do
                echo $i;
            done;

            image

             

             

            • while循環使用(while/do/done)

            while語句結構

            while 條件語句

            do

            action

            done;

            實例1:

            #!/bin/sh
            i=10;
            while [[ $i -gt 5 ]];do
                echo $i;
                ((i--));
            done;

            運行結果:========================

            sh testwhile1.sh
            10
            9
            8
            7
            6

            實例2:(循環讀取文件內容:)

            #!/bin/sh

            while read line;do
                echo $line;
            done < /etc/hosts;

             

            運行結果:===================

            sh testwhile2.sh


            # Do not remove the following line, or various programs
            # that require network functionality will fail.
            127.0.0.1 centos5 localhost.localdomain localhost

            • until循環語句

            語法結構:

            until 條件

            do

            action

            done

            意思是:直到滿足條件,就退出。否則執行action.

            實例(testuntil.sh):

            #!/bin/sh

            a=10;

            until [[ $a -lt 0 ]];do

            echo $a;

            ((a—));

            done;

            結果:

            sh testuntil.sh

            10
            9
            8
            7
            6
            5
            4
            3
            2
            1
            0

            三、shell選擇語句(case、select用法)

            • case選擇語句使用(case/esac)

            語法結構

            case $arg in  
                pattern | sample) # arg in pattern or sample  
                ;;  
                pattern1) # arg in pattern1  
                ;;  
                *) #default  
                ;;  
            esac 

            說明:pattern1 是正則表達式,可以用下面字符:

                             *       任意字串
                             ?       任意字元
                             [abc]   a, b, 或c三字元其中之一
                             [a-n]   從a到n的任一字元
                             |       多重選擇

             

            實例:

            #!/bin/sh 

            case $1 in
            start | begin)
                echo "start something"  
                ;;
            stop | end)
                echo "stop something"  
                ;;
            *)
                echo "Ignorant"  
                ;;
            esac

            運行結果:======================

             

            testcase.sh start
            start something

            • select語句使用方法(產生菜單選擇)

            語法:

            select 變量name  in seq變量

            do

                action

            done

            實例:

            #!/bin/sh 

            select ch in "begin" "end" "exit"
            do
            case $ch in
            "begin")
                echo "start something"  
                ;;
            "end")
                echo "stop something"  
                ;;
            "exit")
                echo "exit"  
                break;
                ;;
            *)
                echo "Ignorant"  
                ;;
            esac
            done;

            運行結果:

            image

            說明:select是循環選擇,一般與case語句使用。

             

             

            以上是shell的流程控制語句,條件,循環,選擇。 歡迎討論交流!

            posted on 2015-01-29 20:03 peakflys 閱讀(106) 評論(0)  編輯 收藏 引用

            <2013年1月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            公告

            人不淡定的時候,就愛表現出來,敲代碼如此,偶爾的靈感亦如此……

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产免费直播| 青青青伊人色综合久久| 要久久爱在线免费观看| 狠狠色综合网站久久久久久久高清 | 国内精品九九久久久精品| 91精品国产综合久久婷婷| 国产成人精品久久综合| 久久婷婷色香五月综合激情| …久久精品99久久香蕉国产| 中文字幕无码av激情不卡久久| 久久亚洲精品人成综合网| 久久精品无码一区二区日韩AV | 国产亚洲色婷婷久久99精品91| 久久免费看黄a级毛片| 久久久久中文字幕| 人妻精品久久久久中文字幕一冢本| 青青草国产精品久久久久| 亚洲色欲久久久综合网| 久久亚洲中文字幕精品一区| 国内精品久久久久影院优| 亚洲精品无码久久久影院相关影片| 大美女久久久久久j久久| 青青青国产精品国产精品久久久久| 国产成人精品久久| 亚洲国产婷婷香蕉久久久久久| 免费观看成人久久网免费观看| 色狠狠久久AV五月综合| 国内精品久久久久久久久电影网 | 四虎国产精品免费久久久 | 99精品久久久久久久婷婷| 久久精品一区二区| 9191精品国产免费久久| 国产精品久久久久久一区二区三区 | 日本亚洲色大成网站WWW久久| 一级做a爰片久久毛片人呢| 久久精品无码一区二区三区| 久久精品国产亚洲沈樵| 亚洲国产成人久久综合碰| 久久久亚洲精品蜜桃臀| 久久九九久精品国产| 久久国产精品波多野结衣AV|