數組是包含多個相同或不同數據類型的集合,數組索引從零開始。本文有 15 個用 bash 操作數組的例子。
當有變量為如下格式的時候,Bash 會自動創建數組。
name[index]=valuename 為數組名稱index 為任意數字,或表達式的最終計算值大于等于零。可以顯式聲明數組通過 declare -a arrayname$ cat arraymanip.sh#! /bin/bashUnix[0]='Debian'Unix[1]='Red hat'Unix[2]='Ubuntu'Unix[3]='Suse' echo ${Unix[1]} $./arraymanip.shRed hat訪問數組元素使用括號,如 ${name[index]}
2. 聲明時初始化數組
通過指定元素列表來聲明數組,就不用一個一個分別初始化數組元素的,用空格隔開在括號里。
Syntax:declare -a arrayname=(element1 element2 element3)如果數組元素包含空格符,用引號括起來。
#! /bin/bash$cat arraymanip.shdeclare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');declare -a 聲明一個數組以及括號中的所有元素為數組元素。
3. 輸出整個 Bash 數組
有多種不同的方法來輸出整個數組。如果索引是 @ 或者 *,則引用數組所有元素。也可以使用循環,遍歷數組中每個元素再輸出。
echo ${Unix[@]} # Add the above echo statement into the arraymanip.sh#./t.shDebian Red hat Ubuntu Suse如果引用數組元素,而不提供索引的話,就是引用數組的第一個元素,即索引為零的元素。
4. Bash 數組長度
可以使用特殊參數 $# 來獲得數組長度。
${#arrayname[@]} gives you the length of the array.
$ cat arraymanip.shdeclare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');echo ${#Unix[@]} #Number of elements in the arrayecho ${#Unix} #Number of characters in the first element of the array.i.e Debian$./arraymanip.sh465. 數組第 n 個元素的長度
${#arrayname[n]} 為數組第 n 個元素的長度。
$cat arraymanip.sh#! /bin/bash Unix[0]='Debian'Unix[1]='Red hat'Unix[2]='Ubuntu'Unix[3]='Suse' echo ${#Unix[3]} # length of the element located at index 3 i.e Suse $./arraymanip.sh46. 指定偏移和長度輸出數組
下面的例子是輸出 2 個數組元素,從第索引為3的元素開始。
$cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'Openlinux');echo ${Unix[@]:3:2} $./arraymanip.shSuse Fedora上面的例子返回里索引為 3 和 4 的元素。索引永遠由零開始。
7. 根據偏移和長度,輸出數組指定元素的一部分
輸出一個數組元素的前四個字符。如例,Ubuntu 是數組索引為 3 的元素,可以指定偏移和長度來獲取數組指定元素的一部分。
$cat arraymanip.sh#! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');echo ${Unix[2]:0:4} ./arraymanip.shUbun上面的例子取出索引為 2 的數組元素的前 4 個字符。
8. 搜索和替換數組元素
下面的例子,在數組元素中搜索 Ubuntu,替換為 SCO Unix
$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); echo ${Unix[@]/Ubuntu/SCO Unix} $./arraymanip.shDebian Red hat SCO Unix Suse Fedora UTS OpenLinux但是,這個例子并沒有永久替換數組內容。
9. 添加元素到已存在的 Bash Array
下面的例子展示了如何添加元素到已存在數組。
$cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Unix=("${Unix[@]}" "AIX" "HP-UX")echo ${Unix[7]} $./arraymanip.shAIX‘AIX’ and ‘HP-UX’ are added in 7th and 8th index respectively.在數組 Unix 中,元素 ‘AIX’ 和 ‘HP-UX’ 添加到第 7 和 第 8 位。
10. 刪除數組元素
unset 用于移除數組元素,unset 與給數組元素賦值為 null 是一樣的效果。
$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); unset Unix[3]echo ${Unix[3]}以上例子,會輸出 null 在索引為 3 的值。下面的例子顯示如果完全從數組中刪除。
$ cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');pos=3Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})echo ${Unix[@]} $./arraymanip.shDebian Red hat Ubuntu Fedora UTS OpenLinux本例中,${Unix[@]:0:$pos} 將輸出 3 個元素從索引為 0 開始。合并以上的輸出。這是刪除數組元素的一個方法。
11. 使用模式 (Patterns) 刪除數組元素
在搜索條件,可以給出模式 (Patterns),存儲其余元素到另外一個數組。
$ cat arraymanip.sh#!/bin/bashdeclare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');declare -a patter=( ${Unix[@]/Red*/} )echo ${patter[@]} $ ./arraymanip.shDebian Ubuntu Suse Fedora以上例子刪除數組元素形如 Red*
12. 復制數組
擴展數組元素,存儲到新的數組中。
#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Linux=("${Unix[@]}")echo ${Linux[@]} $ ./arraymanip.shDebian Red hat Ubuntu Fedora UTS OpenLinux13. 連接兩個 Bash 數組
擴展兩個數組,賦值給新數組。
$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh'); UnixShell=("${Unix[@]}" "${Shell[@]}")echo ${UnixShell[@]}echo ${#UnixShell[@]} $ ./arraymanip.shDebian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh14輸出數組 ‘Unix’ 和 ‘Shell’ 中的所有元素,新數組一共有 14 個元素。
14. 刪除整個數組
使用 unset 刪除整個數組。
$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh'); UnixShell=("${Unix[@]}" "${Shell[@]}")unset UnixShellecho ${#UnixShell[@]} $ ./arraymanip.sh0unset 數組后,數組長度為零。
15. 加載文件內容到數組
可以逐行添加文件內容到數組。
#Example file$ cat logfileWelcometothegeekstuffLinuxUnix $ cat loadcontent.sh#!/bin/bashfilecontent=( `cat "logfile" `) for t in "${filecontent[@]}"doecho $tdoneecho "Read file content!" $ ./loadcontent.shWelcometothegeekstuffLinuxUnixRead file content!以上例子中,數組中的所有元素均利用循環輸出。
新聞熱點
疑難解答