Shell數(shù)組添加元素及注意事項(xiàng)
向Shell數(shù)組添加元素有多種方法,每種方法在使用時(shí)都有一些需要注意的地方,沒有見過(guò)這方面的總結(jié)資料,所以自己總結(jié)一下。
直接下標(biāo)添加
array_name[index]=value
最簡(jiǎn)單的添加方式,直接用下標(biāo)index為數(shù)組元素賦值,在使用時(shí)需要注意的是就是需要確保下標(biāo)index處原本是沒有值的,否則會(huì)替換index處原本的值。
數(shù)組長(zhǎng)度添加
array_name[${#array_name[@]}]=value #或array_name[${#array_name[*]}]=value
以數(shù)組長(zhǎng)度為下標(biāo)添加,每次只能增加一個(gè)元素。但此方法有一個(gè)前提,就是數(shù)組中的元素下標(biāo)必須是連續(xù)的,否則會(huì)存在替換已有元素的風(fēng)險(xiǎn)。例如:
arr=([1]="a" [2]="b") echo ${arr[@]} arr[${#arr[@]}]="c" echo ${arr[@]}
實(shí)際上并有添加元素,而是將下標(biāo)為2的元素值“b”替換為了“c”。
重新創(chuàng)建數(shù)組
array_name=("${array_name[@]}" value1 ... valueN)
這種方式不需要下標(biāo)連續(xù),可同時(shí)添加多個(gè)元素,但是有一些要注意的地方:
首先,使用此方式添加元素后,數(shù)組中原有元素的下標(biāo)會(huì)重置,會(huì)從0開始變成連續(xù)的,如果不希望改變下標(biāo)則不能使用這種方式。
其次,雙引號(hào)不能省略,否則,當(dāng)數(shù)組array_name中存在包含空格的元素時(shí)會(huì)按空格將元素拆分成多個(gè)。
最后,不能將“@”替換為“*”,雖然在輸出數(shù)組元素時(shí)可以相互替換,如果替換為“*”,不加雙引號(hào)時(shí)與“@”的表現(xiàn)一致,加雙引號(hào)時(shí),會(huì)將數(shù)組array_name中的所有元素作為一個(gè)元素添加到數(shù)組中。類似于特殊變量$@和$*的區(qū)別。
#!/bin/bash arr1=() initArray(){ arr1=([2]="a b" [3]=2 [5]=4) } showArray(){ echo "Elements in arr1: ${arr1[@]}" echo "Length of arr1: ${#arr1[@]}" echo "The 3rd elements is: ${arr1[2]}" echo } initArray echo "original arr1:" showArray echo "add new elements 3 and 5" echo "--------------------" echo "use @ without quote" arr1=(${arr1[@]} 3 5) showArray initArray echo "use * without quote" arr1=(${arr1[*]} 3 5) showArray initArray echo "use @ with quote" arr1=("${arr1[@]}" 3 5) showArray initArray echo "use * with quote" arr1=("${arr1[*]}" 3 5) showArray
運(yùn)行結(jié)果為:
original arr1:
Elements in arr1: a b 2 4
Length of arr1: 3
The 3rd elements is: a b
add new elements 3 and 5
--------------------
use @ without quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 6
The 3rd elements is: 2
use * without quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 6
The 3rd elements is: 2
use @ with quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 5
The 3rd elements is: 4
use * with quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 3
The 3rd elements is: 5
賦值運(yùn)算符+=
array_name+=(value1 ... valueN)
這種方式不需要元素下標(biāo)連續(xù),可以添加多個(gè)元素,添加后元素下標(biāo)不會(huì)重置,不存在元素覆蓋的風(fēng)險(xiǎn)。唯一要注意的就是“+=”前后不能有空格,并且后面的待添加元素必須用“()”包圍起來(lái),并且多個(gè)元素用空格分隔。新添加的元素的下標(biāo)取決于原本數(shù)組中最后有值的元素的下標(biāo)。
#!/bin/bash arr1=() initArray(){ arr1=([2]="a b" [3]=2 [5]=4) } showArray(){ echo "Elements in arr1: ${arr1[@]}" echo "Length of arr1: ${#arr1[@]}" echo "The 3rd elements is: ${arr1[2]}" echo } initArray echo "original arr1:" showArray echo "add new elements 3 and 5" echo "--------------------" echo "use += " arr1+=(3 5) showArray echo "The 8th elements is: ${arr1[7]}"
運(yùn)行結(jié)果為:
original arr1:
Elements in arr1: a b 2 4
Length of arr1: 3
The 3rd elements is: a b
add new elements 3 and 5
--------------------
use +=
Elements in arr1: a b 2 4 3 5
Length of arr1: 5
The 3rd elements is: a b
The 8th elements is: 5
數(shù)組中原本的元素沒有改變,新添加的元素下標(biāo)是在最后有值的元素下標(biāo)5的基礎(chǔ)上遞增的。
總結(jié)
添加方式 | 語(yǔ)法 |
可添加多個(gè)元素 |
下標(biāo)必須連續(xù) | 添加后下標(biāo)改變 | 可能覆蓋原有元素 |
直接下標(biāo)添加 | array_name[index]=value | 否 | 否 | 否 | 是 |
數(shù)組長(zhǎng)度添加 |
array_name[${#array_name[@]}]=value 或array_name[${#array_name[*]}]=value |
否 | 是 | 否 | 是 |
重新創(chuàng)建數(shù)組 | array_name=("${array_name[@]}" value1 ... valueN) | 是 | 否 | 是 | 否 |
賦值運(yùn)算符+= | array_name+=(value1 ... valueN) | 是 | 是 | 是 | 否 |
到此這篇關(guān)于Shell數(shù)組添加元素及注意事項(xiàng)的文章就介紹到這了,更多相關(guān)Shell數(shù)組添加元素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux shell txt轉(zhuǎn)換成html的實(shí)現(xiàn)代碼
這篇文章主要介紹了linux shell txt轉(zhuǎn)換成html的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11解析Linux?xfs文件系統(tǒng)stat命令Birth字段為空的原因
這篇文章主要介紹了Linux?xfs文件系統(tǒng)stat命令Birth字段為空的原因探究,stat命令在一些平臺(tái)下Birth字段有值,而在一些平臺(tái)則為空值,這是什么原因呢,下面小編給大家詳細(xì)講解,需要的朋友可以參考下2023-05-05Shell中的函數(shù)、函數(shù)定義、作用域問(wèn)題介紹
這篇文章主要介紹了Shell中的函數(shù)、函數(shù)定義、作用域問(wèn)題介紹,本文講解了Shell中函數(shù)的定義、自定義函數(shù)的例子、作用域問(wèn)題等內(nèi)容,需要的朋友可以參考下2014-11-11shell腳本換行問(wèn)題實(shí)戰(zhàn)記錄
換行相信大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于shell腳本換行問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02一個(gè)簡(jiǎn)單的轉(zhuǎn)換輸出的shell腳本代碼
一個(gè)簡(jiǎn)單的轉(zhuǎn)換輸出的shell腳本,學(xué)習(xí)shell腳本的朋友可以看下實(shí)現(xiàn)方法2013-02-02ssh自動(dòng)登錄的4種實(shí)現(xiàn)方法
這篇文章主要介紹ssh自動(dòng)登錄的幾種實(shí)現(xiàn)方法,需要的朋友可以參考下2013-02-02linux生成(加載)動(dòng)態(tài)庫(kù)靜態(tài)庫(kù)和加載示例方法
這篇文章主要介紹了linux生成(加載)動(dòng)態(tài)庫(kù)靜態(tài)庫(kù)示例方法,大家參考使用2013-11-11