Bash技巧:詳解 for 循環命令的兩種格式的用法

Bash 的 for 複合命令 (compound command) 能夠用於循環執行指定的語句。
該命令有兩種不一樣的格式,查看 man bash 裏面對 for 命令的說明,分別描述以下。express

for name [ [ in [ word ... ] ] ; ] do list ; done

for name [ [ in [ word ... ] ] ; ] do list ; done
The list of words following in is expanded, generating a list of items. The variable name is set to each element of this list in turn, and list is executed each time.
If the in word is omitted, the for command executes list once for each positional parameter that is set. The return status is the exit status of the last command that executes.
If the expansion of the items following in results in an empty list, no commands are executed, and the return status is 0.

這種格式的 for 命令先擴展 word 列表,獲得一個數組,而後把 name 變量依次賦值爲 word 列表的每個元素,每次賦值都執行 list 指定的語句。數組

若是沒有提供 word 列表,則遍歷傳入腳本、或者傳入函數的參數列表。bash

假設有一個 testfor_each.sh 腳本,內容以下:less

#!/bin/bash

for word in This is a sample of "for."; do
    echo -n "$word " | tr a-z A-Z
done
echo

for param; do
    echo -n "$param " | tr A-Z a-z
done
echo

這個腳本先使用 for 命令遍歷 This is a sample of "for." 這個字符串的每個單詞,而後打印單詞內容,用 tr 命令把小寫字母轉換爲大寫字母。函數

以後,繼續使用 for 命令遍歷執行腳本時的參數列表,打印參數值,並把大寫字母轉換爲小寫字母。oop

for param 語句沒有提供 in word 參數,默認會遍歷傳入腳本的位置參數(positional parameters)。this

執行 testfor_each.sh 腳本,結果以下:lua

$ ./testfor_each.sh TEST \"FOR\" COMMAND WITH POSITIONAL PARAMETER
THIS IS A SAMPLE OF FOR.
test "for" command with positional parameter

對這個執行結果有個地方須要注意,在 This is a sample of "for." 這個字符串中,用雙引號把 for. 括起來,可是打印出來的結果沒有帶雙引號,這是由於 bash 在解析的時候,會去掉雙引號。code

這裏的雙引號並非字符串自身的內容,只是用於限定雙引號內部的字符串是一個總體。ci

若是想要打印出雙引號,須要用 \" 來進行轉義,如上面的 \"FOR\",打印結果包含了雙引號。

for (( expr1 ; expr2 ; expr3 )) ; do list ; done

for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETIC EVALUATION.
The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero.
Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression expr3 is evaluated.
If any expression is omitted, it behaves as if it evaluates to 1.
The return value is the exit status of the last command in list that is executed, or false if any of the expres‐sions is invalid.

注意:這種格式的 for 命令裏面,expr1expr2expr3 都是算術表達式,要使用 bash 算術表達式的寫法,具體寫法能夠查看 man bash 裏面的 "ARITHMETIC EVALUATION" 小節和 "((expression))" 表達式的說明。

假設有一個 testfor_expr.sh 腳本,內容以下:

#!/bin/bash

declare count
declare count_str
MAX=10

for ((i = 0; i < MAX; ++i)); do
    ((count+=$i))
    count_str+=$i
done
echo "count = $count"
echo "count_str = $count_str"

執行該腳本的結果以下:

$ ./testfor_expr.sh
count = 45
count_str = 0123456789

能夠看到,要使用 ((count+=$i)) 這種寫法纔是進行算術運算,count 變量值纔會像整數同樣進行加減。

寫爲 count_str+=$i 的形式,實際上是進行字符串拼接。

在上面的兩個小括號裏面引用 MAX 變量值時沒有加 $ 符號,這是容許的。固然,寫成 $MAX 也能夠。

若是要提早跳出循環,可使用 break 命令。查看 man bash 對 break 命令說明以下:

break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n is greater than the number of enclosing loops, all enclosing loops are exited.
The return value is 0 unless n is not greater than or equal to 1.

即,break 命令能夠跳出 for 循環、while 循環、until 循環、和 select 循環。

相關文章
相關標籤/搜索