欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shell數(shù)組添加元素及注意事項(xiàng)

 更新時(shí)間:2020年12月09日 14:48:02   作者:_榮耀之路_  
這篇文章主要介紹了Shell數(shù)組添加元素及注意事項(xiàng),向Shell數(shù)組添加元素有多種方法,每種方法在使用時(shí)都有一些需要注意的地方,感興趣的就一起來(lái)了解一下

向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)文章

最新評(píng)論