天天看點

shell程式設計之數組詳解數組

數組

一 數組介紹

什麼是數組?

數組就是一系列元素的集合,一個數組内可以存放多個元素

為何要用數組?

我們可以用數組将多個元素彙總到一起,避免單獨定義的麻煩

二 數組的使用

2.1 數組的定義
# 方式一:array=(元素1 元素2 元素3)
array=(egon 18 male)

# 方式二:array=([key1]=value1 [key2]=value2 [key3]=value3)
array=([0]=111 [1]="two" [2]=333)

# 方式三:依次指派
array_new[0]=111
array_new[1]=222
array_new[2]="third"

# 方式四:利用執行指令的結果設定數組元素:array=($(指令))  或者  array=(`指令`)
該方式會将指令的結果以空格為分隔符切成多個元素然後指派給數組
[[email protected] ~]# ls /test
a.txt	b.txt
[[email protected] ~]# array3=(`ls /test`)
[[email protected] ~]# declare -a |grep array3
declare -a array3='([0]="a.txt" [1]="b.txt")'


# ps:檢視聲明過的數組
declare -a
           

2.2 通路數組内元素

[[email protected] ~]# array=(egon 18 male)

#1、按照索引通路數組内指定位置的元素
[[email protected] ~]# echo ${array[0]}
egon
[[email protected] ~]# echo ${array[1]}
18
[[email protected] ~]# echo ${array[2]}
male
[[email protected] ~]# echo ${array[-1]}  # 支援負向索引
male


# 2、通路數組内全部元素資訊
[[email protected] ~]# echo ${array[*]}
egon 18 male
[[email protected] ~]# echo ${array[@]}
egon 18 male

# 3、擷取數組元素的長度
[[email protected] ~]# echo ${#array[*]}
3
[[email protected] ~]# echo ${#array[@]}
3

拓展:擷取字元串長度
[[email protected] ~]# x=123
[[email protected] ~]# echo ${#x}
3
           
2.3 修改/添加數組元素

存在下标即修改,不存在就添加

[[email protected] ~]# array=(egon 18 male)
[[email protected] ~]# array[0]="EGON"  # 修改
[[email protected] ~]# array[3]="IT"  # 添加
[[email protected] ~]# declare -a |grep array
declare -a array='([0]="EGON" [1]="18" [2]="male" [3]="IT")'
           
2.4 删除數組元素

指定下标删除指定的值,不指定删除整個數組(即所有值)

[[email protected] ~]# unset array[0]
[[email protected] ~]# echo ${array[*]}
18 male IT
[[email protected] ~]# declare -a |grep array
declare -a array='([1]="18" [2]="male" [3]="IT")'
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# unset array[2]
[[email protected] ~]# declare -a |grep array
declare -a array='([1]="18" [3]="IT")'

[[email protected] ~]# unset array  # 删除整個數組
[[email protected] ~]# echo ${array[*]}
           
2.5 數組内元素的截取
# 截取某一個值的某一部分
echo ${arr[4]:4:2}

# 截取部分值(在數組中擷取某部分值)
echo ${arr[*]:3:3}
[[email protected] ~]# array=(egon 18 male IT 1.80)
[[email protected] ~]# echo ${array[*]:1}    # 從索引1開始,一直到最後
18 male IT 1.80
[[email protected] ~]# echo ${array[*]:1:3}  # 從索引1開始,通路3個元素
18 male IT

[[email protected] ~]# array=(one two three four five fire)
[[email protected] ~]# echo ${array[*]#one}
two three four five fire
[[email protected] ~]# echo ${array[*]#f*e}
one two three four

補充:字元串的截取
[[email protected] ~]# x=123456
[[email protected] ~]# echo ${x:1:2}
           
2.6 數組内容的替換
# 替換某個值的内容
echo ${arr[4]/172/192}

# 在全局中替換(替換數組中所有的相關内容)
echo ${arr[*]/12/192}
[[email protected] ~]# array=(one two three four five fire)

[[email protected] ~]# echo ${array[*]/five/abc}
one two three four abc fire

[[email protected] ~]# echo ${array[*]/f*e/abc}
one two three four abc abc
           

三 關聯數組

數組分為兩種

普通數組:隻能使用整數作為數組索引,我們前面介紹的都是普通數組 declare -a 數組名

關聯數組:可以使用字元串作為數組索引,需要用declare -A聲明

聲明關聯數組

方式一:
[[email protected] ~]# declare -A info=(['name']=shanhe)

方式二:
[[email protected] ~]# declare -A info
[[email protected] ~]# info["name"]="egon"
[[email protected] ~]# info["age"]=18
[[email protected] ~]# info["gender"]="male"

[[email protected] ~]# declare -A |grep info
declare -A info='([gender]="male" [name]="egon" [age]="18" )'
[[email protected] ~]# 
[[email protected] ~]# echo ${info[*]}
male egon 18
[[email protected] ~]# 
[[email protected] ~]# echo ${info["name"]}
egon
           

四 周遊數組

方法一: 利用擷取所有資訊進行周遊 (适用于普通數組與關聯數組)

# 例1
declare -A array
array=(["name"]="egon" ["age"]=18 ["gender"]="male")

for item in ${array[*]}
do 
    echo $item
done

# 例2
array=("egon" 18 "male")
for item in ${array[*]}
do 
    echo $item
done
           

方法二: 通過數組元數的索引進行周遊(适用于普通數組與關聯數組)

# 例1
declare -A array
array=(["name"]="egon" ["age"]=18 ["gender"]="male")

for i in ${!array[*]}  # echo ${!array[*]}  # 擷取的是key資訊:name age gender
do 
    echo "$i:${array[$i]}"
done

# 例2
array=("egon" 18 "male")
for i in ${!array[*]}  # echo ${!array[*]} 直接擷取所有元素的索引資訊
do 
    echo $i
    echo ${array[i]}
done
           

方法三:根據數組長度資訊進行周遊,(适用于普通數組)

array=("egon" 18 "male")

for ((i=0;i<${#array[*]};i++))
do 
    echo "$i:${array[$i]}"
done
方法四:通過while循環周遊數組
# while
num=#{#arr[*]}
while (( $num > 0 ))
do
	echo ${arr[$num]}
	let num--
done