數組是一個包含多個值的變量,這些值可以是相同類型或不同類型。沒有數組大小限制,也沒有要求成員變量被連續索引或連續分配的限制。數組索引從0開始。 |
在bash中,使用以下格式的變量時會自動創建數組:
name[index]=value
- name 是數組的名字。
- index 可以是任何數字或表達式,值必須等於或大於零。
要訪問數組元素,請使用大括號,例如${name[index]}。下面是訪問Unix數組中的第二個元素,以爲數組索引從0開始,所以Unix[1]
就是第二個元素了。
[root@localhost ~]# cat arraymanip.sh #! /bin/bash Unix[0]='Debian' Unix[1]='Red hat' Unix[2]='Ubuntu' Unix[3]='Suse' echo ${Unix[1]}
執行腳本,以下是輸出內容:
[root@localhost ~]# ./arraymain.sh Red hat
不必單獨初始化數組的每個元素,可以通過使用括號指定元素列表(由空格分隔)來聲明和初始化數組。下面是語法:
declare -a arrayname=(element1 element2 element3)
如果元素具有空格字符,需要使用引號:
[root@localhost ~]# cat arraymain2.sh #! /bin/bash declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora') echo ${Unix[4]}
下面是輸出結果:
[root@localhost ~]# ./arraymain2.sh Fedora
declare -a
聲明一個數組,括號中的所有元素都是數組的元素。
有多種方法可以打印整個數組。如果索引號是 @ 或者 *
,則引用數組的所有成員。可以使用bash shell中的循環語句遍歷數組元素並進行打印。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix[0]='Debian' Unix[1]='Red hat' Unix[2]='Ubuntu' Unix[3]='Suse' echo ${Unix[@]}
下面是輸出結果:
[root@localhost ~]# ./arraymain.sh Debian Red hat Ubuntu Suse
可以使用$和#
的特殊參數來獲取數組的長度。${#arrayname[@]}
可以獲取數組長度。
[root@localhost ~]# cat arraymain2.sh #! /bin/bash declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora') echo ${#Unix[@]} #數組中元素的數量 echo ${#Unix} #數組中第一個元素的字符數。
下面是輸出,可以看到第一行輸出參數爲5個。第二行輸出第一個元素的字符數量是6個。
[root@localhost ~]# ./arraymain2.sh 5 6
${#arrayname[n]} 給出數組中的n個元素的長度。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix[0]='Debian' Unix[1]='Red hat' Unix[2]='Ubuntu' Unix[3]='Suse' echo ${#Unix[3]} #位於索引3的元素的長度。
以下是輸出,可以看到輸出index爲3的值爲"Suse"的字符長度是4。
[root@localhost ~]# ./arraymain.sh 4
下面的示例顯示了從名爲Unix的數組中從索引位置3開始提取2個元素的方法。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') echo ${Unix[@]:3:2}
以下是輸出,
[root@localhost ~]# ./arraymain.sh Suse Fedora
上面的示例返回第三、第四個索引的值。索引始終以零開頭。
從數組元素中僅提取前四個元素。例如,獲取數組中的第二個元素,並且獲取這個元素的前三個字符:
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') echo ${Unix[1]:0:3}
以下是輸出:
[root@localhost ~]# ./arraymain.sh Red
下面的示例在數組中搜索Ubuntu,並將其替換爲單詞“FreeBSD”。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') echo ${Unix[@]/Ubuntu/FreeBSD}
以下是輸出:
[root@localhost ~]# ./arraymain.sh Debian Red hat FreeBSD Suse Fedora UTS OpenLinux [root@localhost ~]#
注意,該數組替換,不會寫入到數組裏面。
以下示例顯示了將元素添加到現有數組的方法。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') Unix=("${Unix[@]}" "AIX" "HP-UX") echo ${Unix[@]}
以下是輸出:
[root@localhost ~]# ./arraymain.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux AIX HP-UX
在名爲Unix的數組中,元素“ AIX”和“ HP-UX”分別添加在第7個索引和第8個索引中。並輸出所有數組元素。
unset
用於從數組中刪除元素。unset和分配給元素“Null"值具有相同的效果。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') unset Unix[1] echo ${Unix[@]} echo ${Unix[1]}
以下爲輸出:
[root@localhost ~]# ./arraymain.sh Debian Ubuntu Suse Fedora UTS OpenLinux
上面的腳本將打印整個數組,還有索引爲"1"的值,索引爲“1”的值是null。
以下示例顯示了從數組中完全刪除元素的一種方法,下面還是刪除索引爲1的元素。
[root@localhost ~]# cat arraymain.sh #! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') pos=1 Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))}) echo ${Unix[@]}
以下爲輸出:
[root@localhost ~]# ./arraymain.sh Debian Ubuntu Suse Fedora UTS OpenLinux
在此示例中,${Unix[@]:0:$pos}值獲取第1個索引的元素,,而${Unix[@]:$(($pos + 1))}將從第3個索引到最後一個索引。併合並以上兩個輸出。這是從數組中刪除元素的解決方法之一。
在搜索條件中,可以給出正則表達式,並將剩餘的元素存儲到另一個數組中,如下所示。
[root@localhost ~]# cat arraymain2.sh #! /bin/bash declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora') declare -a pattern=( ${Unix[@]/Red*/} ) echo ${pattern[@]}
以下爲輸出:
[root@localhost ~]# ./arraymain2.sh Debian Ubuntu Suse Fedora
上面的示例中刪除了包含"Red"字符的元素。實際是將"Red*"替換爲空字符。
以下實例是將Unix數組複製到Linux數組中:
[root@localhost ~]# cat arraymain.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux') Linux=("${Unix[@]}") echo ${Linux[@]}
以下爲輸出:
[root@localhost ~]# ./arraymain.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux
展開兩個數組的元素,然後將其分配給新數組:
[root@localhost ~]# cat arraymain.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh'); UnixShell=("${Unix[@]}" "${Shell[@]}") echo ${UnixShell[@]} echo ${#UnixShell[@]}
以下爲輸出:
[root@localhost ~]# ./arraymain.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh 14
該實例同時打印數組“ Unix”和“ Shell”數組的元素,並且新數組的元素數爲14個。
unset用於刪除整個數組。
[root@localhost ~]# cat arraymain.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh'); UnixShell=("${Unix[@]}" "${Shell[@]}") unset UnixShell echo ${#UnixShell[@]}
以下爲輸出:
[root@localhost ~]# ./arraymain.sh 0
unset
數組之後,其長度將爲零,如上所示。
您可以將文件內容逐行加載到數組中。
[root@localhost ~]# cat loadcontent.sh #!/bin/bash file=(`cat ./pic.txt`) for i in "${file[@]}" do echo $i done echo -e "\033[31m Read file content! \033[0m"
以下爲輸出:
[root@localhost ~]# ./loadcontent.sh https://www.linuxprobe.com/wp-content/uploads/2021/01/windows7.png https://www.linuxprobe.com/wp-content/uploads/2016/12/bigdata.jpg https://www.linuxprobe.com/wp-content/uploads/2021/01/write-games-and-learn-python.jpg https://www.linuxprobe.com/wp-content/uploads/2021/01/data-center-inspection.jpg https://www.linuxprobe.com/wp-content/uploads/2020/03/devolop-like-linux-09.jpg Read file content!
在上面的示例中,使用for循環,逐一顯示文件中每行的內容。
數組是一個包含多個值的變量,這些值可以是相同類型或不同類型。沒有數組大小限制,也沒有要求成員變量被連續索引或連續分配的限制。數組索引從0開始。