shell腳本編程學習之路-shell數組

1.數組的介紹

在開發shell腳本時,定義變量採用的形式爲「a=1;b=2;c=3",變量多了再一個一個定義就比較麻煩,而且要是有多個不肯定的變量內容也會難以進行變量控制,因而爲了解決上面的問題數組誕生了。nginx

數組就是有限個元素變量或數據用一個名字命名,而後用編號區分他們的變量的集合,這個名字稱爲數組,編號稱爲數組的下標。組成數組的多個變量稱爲數組的份量,也稱爲數組的元素,有時也稱爲下標變量。簡單的說數組就是數據類型的元素按必定順序排列的集合。shell

2.數組的定義與增刪改查

2.1 數組的定義

方法1:用小括號將變量值括起來賦值給數組變量,每一個變量值之間要用空格進行分隔。數組

array=(value1 value2 value3….)

示例以下:bash

[root@shellbiancheng ~]# array=(1 2 3)
[root@shellbiancheng ~]# echo ${array[*]}
1 2 3

方法2:動態地定義數組變量,並使用命令的輸出結果做爲數組的內容。ide

語法爲:ui

array=($())或者array=(`命令`)

示例以下:code

[root@shellbiancheng jiaobenlianxi]# array=($(ls))
[root@shellbiancheng jiaobenlianxi]# echo ${array[1]}
 1.sh
[root@shellbiancheng jiaobenlianxi]# echo ${array[2]}
a.log
[root@shellbiancheng jiaobenlianxi]# echo ${array[3]}
 array01.sh
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]}
1-100.sh 1.sh a.log array01.sh array02.sh beijingcolor.sh b.log break.sh case01.sh check.sh chkconfigoff.sh chkconfigon.sh color1.sh color.sh for1-1000.sh for1-100_3.sh for1-100.sh for1.sh for2.sh for3.sh for4.sh for5.sh hanshu01.sh hanshu02.sh huafei.sh menufruit.sh nginx.sh piliangxiugai1.sh piliangxiugai2.sh piliangxiugai3.sh suijiwnjian.sh sum1-100.sh test uptime.sh usage.sh user.sh while1.sh while2.sh while3.sh while4.sh while.sh zhuajiu.sh
[root@shellbiancheng jiaobenlianxi]# cat array03.sh
array=(
$(ls)
)

for((i=0;i<${#array[*]};i++))

do
echo ${array[i]}
done

2.2 數組的打印及輸出

(1) 打印數組元素three

語法:echo ${數組名[下標]}ip

[root@shellbiancheng ~]# array=(one two three)
[root@shellbiancheng ~]# echo ${array[0]}
one  打印單個數組元素,當未指定數組下標時,數組的下標從0開始
[root@shellbiancheng ~]# echo ${array[1]}
two
[root@shellbiancheng ~]# echo ${array[2]}
three
[root@shellbiancheng ~]# echo ${array[*]}  使用*或者@符號能夠獲得整個數組的內容。
one two three
[root@shellbiancheng ~]# echo ${array[@]}
one two three

(2) 打印數組元素的個數即獲取數組的長度開發

語法:echo ${#array[]}

[root@shellbiancheng ~]# echo ${array[*]}  
one two three    
[root@shellbiancheng ~]# echo ${#array[*]}
3
[root@shellbiancheng ~]# echo ${#array[@]}
3

(3)數組賦值(瞭解)

語法:數組名[下標]=內容

若是下標不存在,則自動添加一個新的數組元素,若是存在則覆蓋原來的值。

[root@shellbiancheng jiaobenlianxi]# array=(1 2 3)
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]}
1 2 3
[root@shellbiancheng jiaobenlianxi]# array[3]=4  增長下標爲3的數組
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]}
1 2 3 4
[root@shellbiancheng ~]# array[1]=two 修改數組元素
[root@shellbiancheng ~]# echo ${array[*]} 
1 two 3 4

(4) 刪除數組(瞭解)

語法:unset 數組名[下標]

清除相應的數組元素,若是不帶下標,則表示清除整個數組的全部值。

[root@shellbiancheng ~]# array=(1 2 3 4) 
[root@shellbiancheng ~]# unset array[1] 刪除下標爲1的數組
[root@shellbiancheng ~]# echo ${array[*]}
1 3 4
[root@shellbiancheng ~]# unset array  刪除整個數組
[root@shellbiancheng ~]# echo ${array[*]}

[root@shellbiancheng ~]#

(5) 數組內容的截取和替換(瞭解)

截取:

[root@shellbiancheng jiaobenlianxi]# array=(1 2 3 4 5)
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]:1:3}
2 3 4
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]:3:4}
4 5

替換:

[root@shellbiancheng jiaobenlianxi]# echo ${array[*]/3/4} 把數組中的3替換成4臨時替換,原數組未作改變
1 2 4 4 5
[root@shellbiancheng jiaobenlianxi]# array1=(${array[*]/3/4})
[root@shellbiancheng jiaobenlianxi]# echo ${array1[*]}
1 2 4 4 5

2.3 數組腳本開發實踐

(1)範例1:使用循環批量輸出數組元素

方法一:使用for循環語句打印數組元素

[root@shellbiancheng jiaobenlianxi]# cat array01.sh 
#!/bin/bash
array=(
1 2 3 4
)
for ip in ${array[*]}
do
    echo $ip
    sleep 2
done

方法二:使用c語言型的for循環打印數組元素

[root@shellbiancheng jiaobenlianxi]# cat array02.sh 
#!/bin/bash
array=(
1 2 3 4
)
for((i=0;i<${#array[*]};i++))
do
    echo ${array[i]}
    sleep 2
done

方法三:使用while循環語句打印數組元素

[root@shellbiancheng jiaobenlianxi]# cat array03.sh 
#!/bin/bash
array=(
1 2 3 4
)
while((i<${#array[*]}))
do
echo ${array[i]}
((i++))
done

3.Shell數組知識小結

(1)定義:

靜態數組:array=(1 2 3) 空格隔開

動態數組:array=($(ls))

給數組賦值:array[3]=4

(2)打印

打印全部元素:${array[*]}或${array[@]}

打印數組長度:${#array[@]}或${#array[*]}

打印單個元素:${array[i]} i是數組下標

(3) 循環打印的經常使用基本語法

[root@shellbiancheng jiaobenlianxi]# cat array02.sh 
#!/bin/bash
array=(
1 2 3 4
)
for((i=0;i<${#array[*]};i++))
do
echo ${array[i]}
sleep 2
done
相關文章
相關標籤/搜索