數(shù)組是包含多個(gè)相同或不同數(shù)據(jù)類型的集合,數(shù)組索引從零開始。本文有 15 個(gè)用 bash 操作數(shù)組的例子。
當(dāng)有變量為如下格式的時(shí)候,Bash 會(huì)自動(dòng)創(chuàng)建數(shù)組。
name[index]=valuename 為數(shù)組名稱index 為任意數(shù)字,或表達(dá)式的最終計(jì)算值大于等于零。可以顯式聲明數(shù)組通過 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訪問數(shù)組元素使用括號(hào),如 ${name[index]}
2. 聲明時(shí)初始化數(shù)組
通過指定元素列表來聲明數(shù)組,就不用一個(gè)一個(gè)分別初始化數(shù)組元素的,用空格隔開在括號(hào)里。
Syntax:declare -a arrayname=(element1 element2 element3)如果數(shù)組元素包含空格符,用引號(hào)括起來。
#! /bin/bash$cat arraymanip.shdeclare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');declare -a 聲明一個(gè)數(shù)組以及括號(hào)中的所有元素為數(shù)組元素。
3. 輸出整個(gè) Bash 數(shù)組
有多種不同的方法來輸出整個(gè)數(shù)組。如果索引是 @ 或者 *,則引用數(shù)組所有元素。也可以使用循環(huán),遍歷數(shù)組中每個(gè)元素再輸出。
echo ${Unix[@]} # Add the above echo statement into the arraymanip.sh#./t.shDebian Red hat Ubuntu Suse如果引用數(shù)組元素,而不提供索引的話,就是引用數(shù)組的第一個(gè)元素,即索引為零的元素。
4. Bash 數(shù)組長度
可以使用特殊參數(shù) $# 來獲得數(shù)組長度。
${#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. 數(shù)組第 n 個(gè)元素的長度
${#arrayname[n]} 為數(shù)組第 n 個(gè)元素的長度。
$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. 指定偏移和長度輸出數(shù)組
下面的例子是輸出 2 個(gè)數(shù)組元素,從第索引為3的元素開始。
$cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'Openlinux');echo ${Unix[@]:3:2} $./arraymanip.shSuse Fedora上面的例子返回里索引為 3 和 4 的元素。索引永遠(yuǎn)由零開始。
7. 根據(jù)偏移和長度,輸出數(shù)組指定元素的一部分
輸出一個(gè)數(shù)組元素的前四個(gè)字符。如例,Ubuntu 是數(shù)組索引為 3 的元素,可以指定偏移和長度來獲取數(shù)組指定元素的一部分。
$cat arraymanip.sh#! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');echo ${Unix[2]:0:4} ./arraymanip.shUbun上面的例子取出索引為 2 的數(shù)組元素的前 4 個(gè)字符。
8. 搜索和替換數(shù)組元素
下面的例子,在數(shù)組元素中搜索 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但是,這個(gè)例子并沒有永久替換數(shù)組內(nèi)容。
9. 添加元素到已存在的 Bash Array
下面的例子展示了如何添加元素到已存在數(shù)組。
$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.在數(shù)組 Unix 中,元素 ‘AIX’ 和 ‘HP-UX’ 添加到第 7 和 第 8 位。
10. 刪除數(shù)組元素
unset 用于移除數(shù)組元素,unset 與給數(shù)組元素賦值為 null 是一樣的效果。
$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); unset Unix[3]echo ${Unix[3]}以上例子,會(huì)輸出 null 在索引為 3 的值。下面的例子顯示如果完全從數(shù)組中刪除。
$ 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 個(gè)元素從索引為 0 開始。合并以上的輸出。這是刪除數(shù)組元素的一個(gè)方法。
11. 使用模式 (Patterns) 刪除數(shù)組元素
在搜索條件,可以給出模式 (Patterns),存儲(chǔ)其余元素到另外一個(gè)數(shù)組。
$ 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以上例子刪除數(shù)組元素形如 Red*
12. 復(fù)制數(shù)組
擴(kuò)展數(shù)組元素,存儲(chǔ)到新的數(shù)組中。
#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Linux=("${Unix[@]}")echo ${Linux[@]} $ ./arraymanip.shDebian Red hat Ubuntu Fedora UTS OpenLinux13. 連接兩個(gè) Bash 數(shù)組
擴(kuò)展兩個(gè)數(shù)組,賦值給新數(shù)組。
$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輸出數(shù)組 ‘Unix’ 和 ‘Shell’ 中的所有元素,新數(shù)組一共有 14 個(gè)元素。
14. 刪除整個(gè)數(shù)組
使用 unset 刪除整個(gè)數(shù)組。
$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 數(shù)組后,數(shù)組長度為零。
15. 加載文件內(nèi)容到數(shù)組
可以逐行添加文件內(nèi)容到數(shù)組。
#Example file$ cat logfileWelcometothegeekstuffLinuxUnix $ cat loadcontent.sh#!/bin/bashfilecontent=( `cat "logfile" `) for t in "${filecontent[@]}"doecho $tdoneecho "Read file content!" $ ./loadcontent.shWelcometothegeekstuffLinuxUnixRead file content!以上例子中,數(shù)組中的所有元素均利用循環(huán)輸出。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注