Shell編程從入門到精通——數組

在Shell編程中同樣支持數組操作,我們可以在Shell腳本對數組進行增刪改查。

一、定義數組

1. 通過指定元素值來定義數組

在Shell中用戶可以像其他編程語言一樣通過元素值來定義數組,語法如下:
array[key]=value
array表示數組的名稱,key參數表示數組元素的索引,通常是一個整數,value爲元素的值,需要注意的是,數組的下標從零開始

#! /usr/bin/env bash

#指定數組元素值
array[1]=hello
array[3]=world

echo "${array[1]} ${array[3]}"

Output:

$ sh test.sh
hello world

2. 通過declare語句定義數組

使用declare定義數組的語法爲:declare -a array

#! /usr/bin/env bash

#通過declare定義數組
declare -a MyArray
MyArray[0]=hello
MyArray[1]=world

echo "${MyArray[0]} ${MyArray[1]}"

Output:

$ sh test.sh
hello world

另外shell還可以定義關聯數組:declare -A ARRAY_NAME,定義關聯數組 即可自定義索引,可爲字符、字符串等。

#! /usr/bin/env bash

#通過declare定義關聯數組
declare -A MyArray
MyArray[sex]=man
MyArray[name]=random_w
MyArray[age]=18

echo "My name is ${MyArray[name]}, I am ${MyArray[age]} years old, and I am a ${MyArray[sex]}"

Output:

$ sh test.sh
My name is random_w, I am 18 years old, and I am a man

3. 通過元素值集合定義數組

在某些時候,用戶可能需要一次性爲數組的所有元素提供一個值,此時,用戶可以通過元素值集合的形式來定義數組,基本語法如下:
array=( v0 v1 v2 ... vn)
array表示數組的名稱,所有元素以空格隔開。

#! /usr/bin/env bash

#通過集合定義數組
MyArray=(1 2 3 4)
#MyArray[@]表示MyArry的所有元素
echo "${MyArray[@]}"

Output:

$ sh test.sh
1 2 3 4

注意:在使用集合的時候,如果某個值中包含空格,則需要使用單引號或者雙引號將值括起來。

4. 通過鍵值對定義數組

這種方法和通過指定元素值來定義數組有點類似,語法如下:
array=([0]=value0 [1]=value1 [2]=value2 ... [n]=value[n])
在這裏array五爲數組的名稱,等號右邊的圓括號表示數組元素及其值,用這種方式定義數組,索引可以是不連續的。

#! /usr/bin/env bash

#通過集合定義數組
MyArray=([0]=1 [2]=2 [3]=3 [4]=4)
#MyArray[@]表示MyArry的所有元素
echo "${MyArray[@]}"

Output:

$ sh test.sh
1 2 3 4

5.數組和普通變量

在Shell中所有的變量實際上都可以當作數組變量來使用。

#! /usr/bin/env bash

#定義字符串
Str="Hello World!"

echo "${Str[0]}  ${Str[@]} ${Str[*]}"

Output:

$ sh test.sh
Hello World! Hello World! Hello World!

從上面的例子我們可以看出字符串也可以作爲數組來使用,但是該數組只有一個元素。
注意:符號@和*都是通配符,表示匹配的所有元素,所以我們可以使用${array[@]}或者${array[*]表示數組的所有元素。

二、數組的賦值

1. 按索引爲元素賦值

按索引方式賦值和通過指定元素值來定義數組是類似的,語法爲:array[n]=Value
array爲數組的名稱,n爲要賦值的元素索引,Valuen爲對應的元素值。

#! /usr/bin/env bash
#定義數組
Student=( random1 random2 random3)

echo "All Student are: ${Student[@]}"

#給數組賦值
Student[1]=hello
Student[3]=world

echo "All Student are: ${Student[@]}"

Output:

$ sh test.sh
All Student are: random1 random2 random3
All Student are: random1 hello random3 world

2. 通過集合爲數組賦值

通過集合給數組賦值和通過集合定義數組的語法是相同的:array=( v0 v1 v2 ... vn)
當給數組提供一組值的時候,Shell會從第一個元素開始,依次將這些值賦值給每個元素,當新值的數量超過原來的數組的時候,Shell就會在數組末尾添加新的元素,當新的值的個數少於原來的長度,Shell就會將新的值從第一個元素開始賦值,然後刪除超出的元素。

#! /usr/bin/env bash
#定義數組
Student=( random1 random2 random3)

echo "All Student are: ${Student[@]}"

#給數組賦值
Student=( random_w1 random_w2 random_w3 random_w4)

echo "All Student are: ${Student[@]}"

#給數組賦值
Student=( random_w1 random_w2)

echo "All Student are: ${Student[@]}"

Output:

$ sh test.sh
All Student are: random1 random2 random3
All Student are: random_w1 random_w2 random_w3 random_w4
All Student are: random_w1 random_w2

3. 在數組末尾追加新元素

在Shell中,在通過索引給數組元素賦值時,如果指向的索引不存在,則Shell會自動添加一個新的元素,並賦值給數組。

#! /usr/bin/env bash
#定義數組
Student=( random1 random2 random3)

echo "All Student are: ${Student[@]}"

#給數組賦值
Student[3]=random4

echo "All Student are: ${Student[@]}"

Output:

$ sh test.sh
All Student are: random1 random2 random3
All Student are: random1 random2 random3 random4

4. 通過循環爲數組賦值

在實際情況中,比較常見的就是通過循環給數組賦值,下面舉個例子:

#! /usr/bin/env bash

for i in {1..10}
do
    array[$i]=$i
done

echo "${array[@]}"

Output:

$ sh test.sh
1 2 3 4 5 6 7 8 9 10

三、訪問數組

1. 訪問第一個數組元素

前面已經說過,我們和可以通過數組的方式調用字符串,同樣,我們可以使用調用字符串的形式調用數組的第一個元素:

#! /usr/bin/env bash

array=(1 2 3 4)

echo "${array}"

Output:

$ sh test.sh
1

2. 通過下標訪問數組元素

這個我們在前面已經用過了,語法爲:array[n],表示調用數組下標爲n的元素,這裏不再舉例。

3. 計算數組的長度

在Shell中我們可以使用$#來獲取數組的長度。該操作符的基本語法如下:

${#array[@]}或者${#array[*]}

通過$#我們還可以獲取某個數組元素的長度:

${#array[n]}

#! /usr/bin/env bash
#給數組賦值
Student=( random_w1 random_w2 random_w3 random_w4)

echo "All Student are: ${Student[@]}, There are ${#Student[@]} students in all"

Output:

$ sh test.sh
All Student are: random_w1 random_w2 random_w3 random_w4, There are 4 students in all

4. 通過循環遍歷數組元素

這個是通過循環給數組賦值的逆過程:

#! /usr/bin/env bash
#給數組賦值
Student=( random_w1 random_w2 random_w3 random_w4)

for ((i=0;i<${#Student[@]};i++))
do
    echo "Student: ${Student[$i]}"
done

Output:

$ sh test.sh
Student: random_w1
Student: random_w2
Student: random_w3
Student: random_w4

5. 引用所有的數組元素

我們可以通過{array[@]}${array[*]}方式引用數組的所有元素,前面我們已經用過了,這裏就不舉例了。

6. 以切片方式獲取部分數組元素

這裏所說的切片是指截取數組的部分元素或者某個元素的福分內容,例如獲取某個數組的第二個元素開始到第五個元素,獲取切片的語法如下:
${array[@|*]:start:length}
語法中array爲數組的名稱,start爲下標開始的地方,length爲要截取的長度,通過上面的語法我們獲取到的是一個字符串,如果想獲取到的依然是一個數組,只需要給兩邊加上括號即可,如:
(${array[@|*]:start:length})

#! /usr/bin/env bash
#給數組賦值
Release=( Debian Redhat Ubuntu Suse Fedora UTS CentOS)

echo "${Release[@]2:3}"

Release=(${Release[@]:2:3})
for ((i=0;i<${#Release[@]};i++))
do
    echo "${Release[$i]}"
done

Output:

$ sh test.sh
Suse Fedora UTS CentOS
Ubuntu
Suse
Fedora

Shell中除了可以對數組進行切片外,還可以對數組中的元素進行切片,語法如下:
${array[n]:start:length}

#! /usr/bin/env bash
#給數組賦值
Release=( Debian Redhat Ubuntu Suse Fedora UTS CentOS)

#這裏截取CentOS的最後兩個字符
echo "${Release[6]:4:2}"

Output:

$ sh test.sh
OS

7. 數組元素的替換

在Shell中用戶還可以對數組元素進行替換操作,這裏的替換是指將某個數組的部分內容用其他的字符串倆代替,但是並不影響原來數組的值,其基本語法如下:
${array[@|*]/pattern/replacemant}
在上面的語法中,array表示數組的名稱,pattern表示要替換的字符串,replacement表示用來替換的字符串。

#! /usr/bin/env bash

#給數組賦值
Release=( Debian Redhat Ubuntu Suse Fedora UTS CentOS)

echo "New array: ${Release[@]/Ubuntu/random_w}"

echo "Old array: ${Release[@]}"

Output:

$ sh test.sh
New array: Debian Redhat random_w Suse Fedora UTS CentOS
Old array: Debian Redhat Ubuntu Suse Fedora UTS CentOS

四、刪除數組

1.刪除指定數組元素

與刪除其他Shell變量一樣,我們可以使用unset刪除數組的元素:
unset array[n]

#! /usr/bin/env bash

#給數組賦值
Release=( Debian Redhat Ubuntu Suse Fedora UTS CentOS)

unset Release[2]
echo "New array: ${Release[@]}"

Output:

$ sh test.sh
New array: Debian Redhat Suse Fedora UTS CentOS

2. 刪除整個數組

同樣我們可是使用unset命令刪除數組
unset array

#! /usr/bin/env bash

#給數組賦值
Release=( Debian Redhat Ubuntu Suse Fedora UTS CentOS)

unset Release
echo "New array: ${Release[@]}"

Output:

$ sh test.sh
New array:

五、數組的其他操作

1. 複製數組

在Shell中用戶可以通過下面的方式實現數組的賦值:
newarray=("$array[@]")

#! /usr/bin/env bash

#給數組賦值
Release=( Debian Redhat Ubuntu Suse Fedora UTS CentOS)

Release2=("${Release[@]}")
echo "Release2: ${Release2[@]}"

Output:

$ sh test.sh
Release2: Debian Redhat Ubuntu Suse Fedora UTS CentOS

2. 連接數組

連接兩個數組的方式也很簡單,語法如下:
("$array1[@]" "$array2[@]")

#! /usr/bin/env bash

#給數組賦值
Release1=( Debian Redhat Ubuntu)
Release2=( Suse Fedora UTS CentOS)

Release=("${Release1[@]}" "${Release2[@]}")
echo "Release2: ${Release[@]}"
```bash
$ sh test.sh
Release2: Debian Redhat Ubuntu Suse Fedora UTS CentOS

3. 加載文件內容到數組

在Shell中,用戶可以將普通文本的內容直接加載到數組,文件的每一行是數組的一個元素:

[root@random_wz ~]# cat student.txt
random_w1
random_w2
random_w3
random_w4
#! /usr/bin/env bash

#給數組賦值
Student=(`cat "student.txt"`)

echo "Student: ${Student[@]}"

Output:

[root@random_wz ~]# sh test.sh
Student: random_w1 random_w2 random_w3 random_w4
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章