shell腳本編程之更多結(jié)構(gòu)化命令

技術(shù)交流QQ群:1027579432,歡迎你的加入!

歡迎關(guān)注我的微信公眾號(hào):CurryCoder的程序人生

本教程使用Linux發(fā)行版Centos7.0系統(tǒng),請(qǐng)您注意~

1.for命令

  • bash shell提供了for命令,允許你創(chuàng)建一個(gè)遍歷一系列值的循環(huán)。每次迭代都使用其中的一個(gè)值來(lái)執(zhí)行已定義好的一組命令,for命令的基本格式如下所示:
    for var in list
    do 
        命令...
    done
    
  • 在list參數(shù)中,需要提供迭代需要的一系列值??梢酝ㄟ^(guò)幾種不同的方法指定列表中的值。
  • do和done語(yǔ)句中之間輸入的命令可以是一條或者多條標(biāo)準(zhǔn)的bash shell命令。
  • 也可以將do語(yǔ)句和for語(yǔ)句放在同一行,但是必須用分號(hào)將其同列表中的值分開(kāi):for var in list; do
1.1 讀取列表中的值
  • for命令最基本的用法就是遍歷for命令自身定義的一系列值,如下例所示:
    #!/bin/bash
    
    
    for li in A B C D E F G
    do
        echo The next state is $li
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana.sh
    The next state is A
    The next state is B
    The next state is C
    The next state is D
    The next state is E
    The next state is F
    The next state is G
    
1.2 讀取列表中的復(fù)雜值
  • 當(dāng)shell看到list列表中的單引號(hào)并嘗試使用它們來(lái)定義一個(gè)單獨(dú)的數(shù)據(jù)值時(shí),結(jié)果會(huì)一團(tuán)糟。如下例所示:
    #!/bin/bash
    
    for var in I don't know if this'll work
    do 
        echo "word:$var"
    done
    
    # 結(jié)果
    word:I
    word:dont know if thisll
    word:work
    
  • 兩種解決方法:
    • 使用轉(zhuǎn)義字符來(lái)將單引號(hào)進(jìn)行轉(zhuǎn)義;
    • 使用雙引號(hào)來(lái)定義用到單引號(hào)的值;
    #!/bin/bash
    
    for var in I don\'t know if "this'll" work
    do 
        echo "word:$var"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana1.sh 
    word:I
    word:don't
    word:know
    word:if
    word:this'll
    word:work
    
  • 注意:for循環(huán)假定每個(gè)值都是用空格分割的,如果有包含空格的數(shù)據(jù)值,就會(huì)陷入麻煩。如下例所示:
    #!/bin/bash
    
    
    for var in New York New Mexico North Carolina
    do
        echo "Now going to $var"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana2.sh 
    Now going to New
    Now going to York
    Now going to New
    Now going to Mexico
    Now going to North
    Now going to Carolina
    
  • for命令用空格來(lái)劃分列表中的每個(gè)值,如果在單獨(dú)的數(shù)據(jù)值中有空格,就必須使用雙引號(hào)將這些值圈起來(lái),在某個(gè)值兩邊使用雙引號(hào)時(shí),shell并不會(huì)將雙引號(hào)當(dāng)成值的一部分。如下例所示:
    #!/bin/bash
    
    
    for var in "New York" "New Mexico" "North Carolina"
    do
        echo "Now going to $var"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana2.sh 
    Now going to New York
    Now going to New Mexico
    Now going to North Carolina
    
1.3 從變量讀取列表list
  • 通常情況下,是將一系列值都集中存儲(chǔ)在一個(gè)變量,然后需要遍歷變量中的整個(gè)列表,如下例所示:
    #!/bin/bash
    
    list="Red Green Blue"
    
    list=$list" Blank"  # 使用賦值語(yǔ)句向$list變量包含的已有列表中添加一個(gè)值Blank,這是向變量中存儲(chǔ)的已有文本字符串尾部添加文本的常用方法
    for var in $list
    do
        echo "The current color is $var"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana3.sh 
    The current color is Red
    The current color is Green
    The current color is Blue
    The current color is Blank
    
1.4 從命令讀取值
  • 生成列表中所需值的另一途徑就是使用命令的輸出??梢允褂?strong>命令替換來(lái)執(zhí)行任何能產(chǎn)生輸出的命令,然后在for命令中使用該命令的輸出。如下例所示:
    #!/bin/bash
    
    file="color"
    
    
    for c in $(cat $file)
    do
        echo "beautiful color:$c"
    done
    
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana4.sh 
    beautiful color:Red
    beautiful color:Green
    beautiful color:Bule
    beautiful color:Pink
    
  • 上述情況中每種顏色都在單獨(dú)的一行,而不是通過(guò)空格分隔的。但是,這并沒(méi)有解決數(shù)據(jù)中用空格的情況。
1.5 更改字段分隔符
  • 造成for命令中以空格為分隔符的原因是特殊的環(huán)境變量IFS(內(nèi)部字段分隔符),IFS環(huán)境變量定義了bash shell用作字符分隔符的一些列字符。默認(rèn)情況下,bash shell會(huì)將下面的字符當(dāng)成字段分隔符:
    • 空格
    • 制表符
    • 換行符
  • 如果bash shell在數(shù)據(jù)中看到了這些字符中的任意一個(gè),它就會(huì)假定這表明了列表中一個(gè)新數(shù)據(jù)字段的開(kāi)始。這會(huì)導(dǎo)致在處理含有空格的數(shù)據(jù)時(shí),顯得十分麻煩。解決方法:可以在shell腳本中臨時(shí)更改IFS環(huán)境變量的值來(lái)限制被bash shell當(dāng)作字段分隔符的字符,如下例所示:
    #!/bin/bash
    
    file="color"
    
    IFS=$'\n'   # 告訴bash shell在數(shù)據(jù)值中忽略空格和制表符,僅識(shí)別換行符!
    
    for c in $(cat $file)
    do
        echo "beautiful color:$c"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana4.sh 
    beautiful color:Red
    beautiful color:Green
    beautiful color:Bule
    beautiful color:Pink
    beautiful color:Deep Purple
    
  • 在處理代碼量較大的腳本時(shí),可能在一個(gè)地方需要修改IFS值。在腳本的其他地方繼續(xù)沿用默認(rèn)的IFS值。一個(gè)可行的方法是在改變IFS之前保存原來(lái)的IFS值,之后再恢復(fù)它,這樣保證了在腳本的后續(xù)操作中繼續(xù)使用的是默認(rèn)的IFS值。
    IFS.OLD=$IFS
    
    IFS=$'\n'
    <在代碼中使用新的IFS值>
    IFS=$IFS.OLD
    
  • IFS環(huán)境變量的其他一些絕妙用法,假如你要遍歷一個(gè)文件中用冒號(hào)分隔的值(比如/etc/passwd文件),你需要做的就是將IFS設(shè)置為冒號(hào)IFS=:。
  • 如果要指定多個(gè)IFS字符,只要將它們?cè)谫x值行串起來(lái)就行,如IFS=$'\n':;"。
1.6 用通配符讀取目錄
  • 可以用for命令來(lái)自動(dòng)遍歷目錄中的文件,進(jìn)行此操作時(shí),必須在文件名或路徑名中使用通配符。它會(huì)強(qiáng)制shell使用文件擴(kuò)展匹配。文件擴(kuò)展匹配是生成匹配指定通配符的文件名或路徑名的過(guò)程。如下例所示:
    #!/bin/bash
    
    for file in /home/njust/tutorials/*
    do
    
        if [ -d "$file" ]  # 在linux中文件名或目錄名中包含空格是合法的,要解決這個(gè)問(wèn)題可以將$file變量用雙引號(hào)圈起來(lái)。如果不這樣的話遇到含空格的目錄名或文件名時(shí)會(huì)報(bào)錯(cuò)!
        then
            echo "$file is a directory"
        elif [ -f "$file" ]
        then
            echo "$file is a file"
        fi
    done
    
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana5.sh 
    /home/njust/tutorials/case.sh is a file
    /home/njust/tutorials/color is a file
    /home/njust/tutorials/curry.sh is a file
    /home/njust/tutorials/dana is a file
    /home/njust/tutorials/dana1.sh is a file
    /home/njust/tutorials/dana2.sh is a file
    /home/njust/tutorials/dana3.sh is a file
    /home/njust/tutorials/dana4.sh is a file
    /home/njust/tutorials/dana5.sh is a file
    /home/njust/tutorials/dana.sh is a file
    
  • 也可以在for命令中列出多個(gè)目錄通配符,將目錄查找和列表合并進(jìn)同一個(gè)for語(yǔ)句,如下例所示:
    #!/bin/bash
    
    for file in /home/njust/.b* /home/njust/tutorials
    do
    
        if [ -d "$file" ]
        then
            echo "$file is a directory"
        elif [ -f "$file" ]
        then
            echo "$file is a file"
        else
            echo "$file doesn't exist"
        fi
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana6.sh 
    /home/njust/.bash_history is a file
    /home/njust/.bash_logout is a file
    /home/njust/.bash_profile is a file
    /home/njust/.bashrc is a file
    /home/njust/tutorials is a directory
    

2.C語(yǔ)言風(fēng)格的for命令

  • bash shell也支持一種for循環(huán),它看起來(lái)與C語(yǔ)言風(fēng)格的for循環(huán)類似,但有一些細(xì)微的不同。bash中C語(yǔ)言風(fēng)格的for循環(huán)基本格式如下
    for (( 變量初始化; 條件; 變量迭代變化 ))
    
  • 注意:有些部分并沒(méi)有遵循bash shell標(biāo)準(zhǔn)的for命令
    • 變量賦值可以有空格;
    • 條件中的變量不以美元符開(kāi)頭;
    • 迭代過(guò)程的算式未使用expr命令格式;
  • 以下是在bash shell程序中使用C語(yǔ)言風(fēng)格的for命令:
    #!/bin/bash
    
    
    for (( i = 1; i <= 10; i++ )) 
    do 
        echo "The next number is $i"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana7.sh 
    The next number is 1
    The next number is 2
    The next number is 3
    The next number is 4
    The next number is 5
    The next number is 6
    The next number is 7
    The next number is 8
    The next number is 9
    The next number is 10
    
2.1 使用多個(gè)變量
  • C語(yǔ)言風(fēng)格的for命令也允許為迭代使用多個(gè)變量。循環(huán)會(huì)單獨(dú)處理每個(gè)變量,可以為每個(gè)變量定義不同的迭代過(guò)程,盡管可以使用多個(gè)變量,但只能在for循環(huán)中定義一種條件。
    #!/bin/bash
    
    
    # mutiple variables
    
    for (( a = 1, b = 10; a <= 10; a++, b-- ))
    do 
        echo "$a - $b"
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana8.sh 
    1 - 10
    2 - 9
    3 - 8
    4 - 7
    5 - 6
    6 - 5
    7 - 4
    8 - 3
    9 - 2
    10 - 1
    

3.while命令

  • while命令允許定義一個(gè)要測(cè)試的命令,然后循環(huán)執(zhí)行一組命令,只要定義的測(cè)試命令返回的是退出狀態(tài)碼0,它會(huì)在每次迭代的一開(kāi)始測(cè)試test命令。在test命令返回非零退出狀態(tài)碼時(shí),while命令會(huì)停止執(zhí)行那組命令。
3.1 while的基本格式
  • while命令的格式如下:
    while test 命令
    do
        命令
    done
    
  • while命令的關(guān)鍵點(diǎn):所指定的test 命令的退出狀態(tài)碼必須隨著循環(huán)中運(yùn)行的命令而改變。如果退出狀態(tài)碼不發(fā)生改變,while循環(huán)就會(huì)一直不停地執(zhí)行下去。最常見(jiàn)的test 命令的用法是用方括號(hào)檢查循環(huán)命令中用到的shell變量的值,如下所示:
    #!/bin/bash
    
    var1=10
    while [ $var1 -gt 0 ]
    do 
        echo $var1
        var1=$[ $var1 - 1 ]
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana9.sh 
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    
3.2 使用多個(gè)測(cè)試命令
  • while命令允許你在while語(yǔ)句行定義多個(gè)測(cè)試命令。只有最后一個(gè)測(cè)試命令的退出狀態(tài)碼會(huì)被用來(lái)決定什么時(shí)候結(jié)束循環(huán)。如下例所示:
    #!/bin/bash
    
    var=10
    
    while echo $var
        [ $var  -ge 0 ]
    do
        echo "This is inside in the loop"
        var=$[ $var - 1 ]
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana10.sh 
    10
    This is inside in the loop
    9
    This is inside in the loop
    8
    This is inside in the loop
    7
    This is inside in the loop
    6
    This is inside in the loop
    5
    This is inside in the loop
    4
    This is inside in the loop
    3
    This is inside in the loop
    2
    This is inside in the loop
    1
    This is inside in the loop
    0
    This is inside in the loop
    -1
    
  • 在含有多個(gè)命令的while語(yǔ)句中,在每次迭代中所有測(cè)試命令都會(huì)被執(zhí)行,包括測(cè)試命令失敗的最后一次迭代。注意:指定多個(gè)測(cè)試命令時(shí),每個(gè)測(cè)試命令都出現(xiàn)在單獨(dú)的一行。

4.until命令

  • until命令與while命令工作方式相反,until命令要求你指定一個(gè)通常返回非零狀態(tài)碼的測(cè)試命令。只有測(cè)試命令的退出狀態(tài)碼非零,bash shell才會(huì)執(zhí)行循環(huán)中列出的命令。一旦測(cè)試命令返回退出狀態(tài)碼0,循環(huán)就結(jié)束了。until命令的基本格式如下所示:
    until test 命令
    do
        命令
    done
    
  • 可以在until命令語(yǔ)句中放入多個(gè)測(cè)試命令,只有最后一個(gè)命令的退出狀態(tài)碼決定了了bash shell是否執(zhí)行已定義的命令。如下例所示:
    #!/bin/bash
    
    
    var=100
    
    until [ $var -eq 0 ]
    do
        echo $var
        var=$[ $var - 25 ]
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana11.sh 
    100
    75
    50
    25
    
  • 與while命令相似,until命令在使用多個(gè)測(cè)試命令時(shí)要注意。shell會(huì)執(zhí)行指定的多個(gè)測(cè)試命令,只有在最后一個(gè)命令成立時(shí)才會(huì)停止。如下例所示:
    #!/bin/bash
    
    
    var=100
    
    until echo $var
        [ $var -eq 0 ]
    do
        echo "Inside the loop: $var"
        var=$[ $var - 25 ]
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana12.sh 
    100
    Inside the loop: 100
    75
    Inside the loop: 75
    50
    Inside the loop: 50
    25
    Inside the loop: 25
    0
    

5.嵌套循環(huán)

  • 循環(huán)語(yǔ)句可以在循環(huán)內(nèi)使用任意類型的命令,包括其他循環(huán)命令。這種循環(huán)稱為嵌套循環(huán)。注意:使用嵌套循環(huán)時(shí),在迭代中使用迭代,與命令運(yùn)行的次數(shù)是乘積關(guān)系。如下例所示:
    #!/bin/bash
    
    for (( a = 1; a <= 3; a++ ))
    do
        echo "Starting loop $a:"
        for (( b = 1; b <= 3; b++ ))
        do
            echo "  Inside loop:$b"
        done
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana13.sh 
    Starting loop 1:
    Inside loop:1
    Inside loop:2
    Inside loop:3
    Starting loop 2:
    Inside loop:1
    Inside loop:2
    Inside loop:3
    Starting loop 3:
    Inside loop:1
    Inside loop:2
    Inside loop:3
    
  • 在混用循環(huán)命令時(shí)也一樣,比如在while循環(huán)內(nèi)部放置一個(gè)for循環(huán)。如下例所示:
    #!/bin/bash
    
    
    var1=5
    
    while [ $var1 -ge 0 ]
    do
        echo "Outer loop: $var1"
        for (( var2 = 1; $var2 < 3; var2++ ))
        do
            var3=$[ $var1 * $var2 ]
            echo "  Inner loop: $var1 * $var2 = $var3"
        done
        var1=$[ $var1 - 1 ]
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana14.sh 
    Outer loop: 5
    Inner loop: 5 * 1 = 5
    Inner loop: 5 * 2 = 10
    Outer loop: 4
    Inner loop: 4 * 1 = 4
    Inner loop: 4 * 2 = 8
    Outer loop: 3
    Inner loop: 3 * 1 = 3
    Inner loop: 3 * 2 = 6
    Outer loop: 2
    Inner loop: 2 * 1 = 2
    Inner loop: 2 * 2 = 4
    Outer loop: 1
    Inner loop: 1 * 1 = 1
    Inner loop: 1 * 2 = 2
    Outer loop: 0
    Inner loop: 0 * 1 = 0
    Inner loop: 0 * 2 = 0
    

6.循環(huán)處理文件數(shù)據(jù)

  • 通常必須遍歷存儲(chǔ)在文件中的數(shù)據(jù),這要求結(jié)合已講過(guò)的兩種技術(shù):
    • 使用嵌套循環(huán);
    • 修改IFS環(huán)境變量;
  • 通過(guò)修改IFS環(huán)境變量,就能強(qiáng)制for命令將文件中的每行都當(dāng)成單獨(dú)的一個(gè)條目來(lái)處理,即使數(shù)據(jù)中有空格也是如此。一旦從文件中提取出了單獨(dú)的行,可能需要再次利用循環(huán)來(lái)提取行中的數(shù)據(jù)。典型的案例是處理/etc/passwd文件中的數(shù)據(jù),逐行遍歷/etc/passwd文件,并將IFS變量的值改成冒號(hào),這樣就能分割開(kāi)每行中的各個(gè)數(shù)據(jù)段。如下例所示:
    #!/bin/bash
    
    IFS.OLD=$IFS
    
    IFS=$'\n'
    for var in $( cat /etc/passwd )
    do
        echo "Values in $var -"
        IFS=:
        for v in $var
        do
            echo "  $v"
        done
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana15.sh 
    Values in root:x:0:0:root:/root:/bin/bash -
    root
    x
    0
    0
    root
    /root
    /bin/bash
    

7.控制循環(huán)

7.1 break命令
  • break語(yǔ)句是退出循環(huán)的一個(gè)簡(jiǎn)單方法。可以使用break命令來(lái)退出任意類型的循環(huán),包括while循環(huán)和until循環(huán)。
  • 跳出單個(gè)循環(huán):在shell執(zhí)行break命令時(shí),它會(huì)嘗試跳出當(dāng)前正在執(zhí)行的循環(huán)。
    #!/bin/bash
    
    for var in 1 2 3 4 5 6 7 8 9 10
    do
        if [ $var -eq 5 ]
        then
            break
        fi
        echo "Current number: $var"
    done
    echo "The for loop is completed"
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana16.sh 
    Current number: 1
    Current number: 2
    Current number: 3
    Current number: 4
    The for loop is completed
    
  • 跳出單個(gè)循環(huán)的情景同樣也適用于while和until循環(huán),如下例所示:
    #!/bin/bash
    
    
    var=1
    
    while [ $var -lt 10 ]
    do
        if [ $var -eq 5 ]
        then
            break
        fi
        echo "Iteration: $var"
        var=$[ $var + 1 ]
    done
    
    echo "The while loop is completed"
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana17.sh 
    Iteration: 1
    Iteration: 2
    Iteration: 3
    Iteration: 4
    The while loop is completed
    
  • 跳出內(nèi)部循環(huán):在處理多個(gè)循環(huán)時(shí),break命令會(huì)自動(dòng)終止你所在的最內(nèi)層的循環(huán)。
    #!/bin/bash
    
    
    for (( a =  1; a < 4; a++ ))
    do
        echo "Outer loop:$a"
        for (( b = 1; b < 100; b++ ))
        do
            if [ $b -eq 5 ]
            then
                break
            fi
            echo "  Inner loop:$b"
        done
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana18.sh 
    Outer loop:1
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
    Outer loop:2
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
    Outer loop:3
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
    
  • 跳出外部循環(huán):有時(shí)候你在內(nèi)部循環(huán),但需要停止外部循環(huán)。break命令接收單個(gè)命令行參數(shù)值:break n。n指定了要跳出的循環(huán)層級(jí),默認(rèn)情況下,n等于1,表示跳出的是當(dāng)前的循環(huán)。如果n設(shè)置為2,break命令會(huì)停止下一級(jí)的外部循環(huán)。如下例所示:
    #!/bin/bash
    
    for (( a = 1; a < 4; a++ ))
    do
        echo "Outer loop:$a"
        for (( b = 1; b < 100; b++ ))
        do
            if [ $b -gt 4 ]
            then
                break 2
            fi
            echo "  Inner loop:$b"
        done
    done 
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana19.sh 
    Outer loop:1
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
    
7.2 continue命令
  • continue命令可以提前中止某次循環(huán)中的命令,但并不會(huì)完全終止整個(gè)循環(huán)??梢栽谘h(huán)內(nèi)部設(shè)置shell不執(zhí)行命令的條件。如下例所示:
    #!/bin/bash
    
    for (( a = 1; a < 15; a++ ))
    do
        if [ $a -gt 5 ] && [ $a -lt 10 ]
        then
            continue
        fi
        echo "Iteration number: $a"
    done
    
    # 結(jié)果
    Iteration number: 1
    Iteration number: 2
    Iteration number: 3
    Iteration number: 4
    Iteration number: 5
    Iteration number: 10
    Iteration number: 11
    Iteration number: 12
    Iteration number: 13
    Iteration number: 14
    
  • 也可以在while或until循環(huán)中使用continue命令,但是特別小心。當(dāng)shell執(zhí)行continue命令時(shí),它會(huì)跳過(guò)剩余的命令。如果你在其中某個(gè)條件里對(duì)測(cè)試條件變量進(jìn)行增值,問(wèn)題就會(huì)出現(xiàn)
    #!/bin/bash
    
    var1=0
    
    while echo "while iteration: $var1"
          [ $var1 -lt 15 ]
    do
        if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
        then
            continue  # 當(dāng)shell執(zhí)行continue命令時(shí),它跳過(guò)了while循環(huán)中余下的命令。
        fi
        echo "  Inside iteration number:$var1"
        var1=$[ $var1 + 1 ]
    done
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana21.sh 
    while iteration: 0
    Inside iteration number:0
    while iteration: 1
    Inside iteration number:1
    while iteration: 2
    Inside iteration number:2
    while iteration: 3
    Inside iteration number:3
    while iteration: 4
    Inside iteration number:4
    while iteration: 5
    Inside iteration number:5
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    
  • 和break命令類似,continue命令也允許通過(guò)命令行參數(shù)指定要繼續(xù)執(zhí)行哪一級(jí)循環(huán):continue n。其中n定義了要繼續(xù)的循環(huán)層級(jí),如下例所示:
    #!/bin/bash
    
    
    for (( a = 1; a <= 5; a++ ))
    do
        echo "Iteration: $a"
        for (( b = 1; b < 3; b++ ))
        do 
            if [ $a -gt 2 ] && [ $a -lt 4 ]
            then
                continue 2 
            fi
            var=$[ $a * $b ]
            echo "  The result of $a * $b is $var"
        done
    done 
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana22.sh 
    Iteration: 1
    The result of 1 * 1 is 1
    The result of 1 * 2 is 2
    Iteration: 2
    The result of 2 * 1 is 2
    The result of 2 * 2 is 4
    Iteration: 3
    Iteration: 4
    The result of 4 * 1 is 4
    The result of 4 * 2 is 8
    Iteration: 5
    The result of 5 * 1 is 5
    The result of 5 * 2 is 10
    

8.處理循環(huán)的輸出

  • 在shell腳本中,可以對(duì)循環(huán)的輸出使用管道或進(jìn)行重定向。可以通過(guò)在done命令后添加一個(gè)處理命令來(lái)實(shí)現(xiàn)。如下例所示:
    #!/bin/bash
    
    
    for file in /home/njust/*
    do
        if [ -d "$file" ]
        then
            echo "$file is a directory"
        else
            echo "$file is a file"
        fi
    done > output.txt
    
    
    # 結(jié)果
    [njust@njust tutorials]$ cat output.txt 
    /home/njust/sentinel is a file
    /home/njust/tutorials is a directory
    /home/njust/公共 is a directory
    /home/njust/模板 is a directory
    /home/njust/視頻 is a directory
    /home/njust/圖片 is a directory
    /home/njust/文檔 is a directory
    /home/njust/下載 is a directory
    /home/njust/音樂(lè) is a directory
    /home/njust/桌面 is a directory
    
  • 上述方法同樣適用于將循環(huán)的結(jié)果通過(guò)管道傳給另一個(gè)命令,如下例所示:
    #!/bin/bash
    
    for state in "North Dakota" Pink Blue Green Red
    do
        echo "$state is the next place to go"
    done | sort
    
    echo "This completes our travels"
    
    
    # 結(jié)果
    [njust@njust tutorials]$ ./dana24.sh 
    Blue is the next place to go
    Green is the next place to go
    North Dakota is the next place to go
    Pink is the next place to go
    Red is the next place to go
    This completes our travels
    

9.循環(huán)實(shí)例

  • 查找可執(zhí)行文件
    #!/bin/bash
    
    
    IFS=:
    
    for folder in $PATH
    
    do
        echo "$folder"
        for file in $folder/*
        do
            if [ -x $file ]
            then
                echo "  $file"
            fi
        done
    done
    
    # 結(jié)果
    /sbin/vgextend
    /sbin/vgimport
    /sbin/vgimportclone
    /sbin/vgmerge
    /sbin/vgmknodes
    /sbin/vgreduce
    /sbin/vgremove
    /sbin/vgrename
    /sbin/vgs
    /sbin/vgscan
    /sbin/vgsplit
    /sbin/vigr
    
  • 創(chuàng)建多個(gè)用戶賬戶
    #!/bin/bash
    
    
    input="users.txt"  # users.txt文件需提前創(chuàng)建,文件內(nèi)容為:userid,username
    
    while IFS=',' read -r userid name
    do
        echo "adding $userid"
        useradd -c "$name" -m $userid
    
    done < "$input"
    
    # 結(jié)果,必須用root用戶才能運(yùn)行此腳本,因?yàn)閡seradd命令需要root權(quán)限!
    [root@njust tutorials]# ./dana26.sh
    adding C001
    adding D002
    adding H003
    adding J004
    
    # 驗(yàn)證
    [root@njust tutorials]# tail /etc/passwd
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    tcpdump:x:72:72::/:/sbin/nologin
    njust:x:1000:1000:njust:/home/njust:/bin/bash
    shenchao:x:1001:1001::/home/shenchao:/bin/bash
    userid:x:1002:1003:name:/home/userid:/bin/bash
    C001:x:1003:1004:curry:/home/C001:/bin/bash
    D002:x:1004:1005:durant:/home/D002:/bin/bash
    H003:x:1005:1006:harden:/home/H003:/bin/bash
    J004:x:1006:1007:james:/home/J004:/bin/bash
    

10.資料下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,717評(píng)論 0 5
  • 介紹能夠控制 shell 腳本流程的結(jié)構(gòu)化命令 更多精彩 更多技術(shù)博客,請(qǐng)移步 IT人才終生實(shí)訓(xùn)與職業(yè)進(jìn)階平臺(tái) -...
    asing1elife閱讀 591評(píng)論 2 5
  • 本篇內(nèi)容均摘自《Linux命令行與shell腳本編程大全》,個(gè)人認(rèn)為需要重點(diǎn)學(xué)習(xí)的章節(jié)?!久赓M(fèi)】Linux命令行與...
    生信start_site閱讀 373評(píng)論 0 0
  • 流程控制 if 單條件 雙分支 多分支 case case支持glob風(fēng)格的通配符: *: 任意長(zhǎng)度任意字符?: ...
    毛利卷卷發(fā)閱讀 824評(píng)論 0 1
  • 大概因?yàn)楫厴I(yè)的學(xué)校還不錯(cuò),又干著實(shí)際上苦逼但外人看來(lái)高大上的創(chuàng)業(yè),網(wǎng)絡(luò)上和現(xiàn)實(shí)中經(jīng)常有人向我取經(jīng)。 小A是我的鄰居...
    花花世界213閱讀 187評(píng)論 0 0

友情鏈接更多精彩內(nèi)容