SHELL腳本--shell數組基礎


bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html


數組和變量的區別是:變量在內存中占用的空間是離散的,數組在內存中是先開辟一段連續的大內存空間,隨后數組中的每個元素都放入數組內存中。數組元素使用數組index標識。

bash里有兩種數組:普通數組和關聯數組。普通數組只能使用整型數值作為數組索引,關聯數組可以使用字符串作為索引。所謂的關聯數組,它的另外三種稱呼:字典(dict)、hash結構和映射(map),是一種key和value一 一對應的關系。

1.9.1 普通數組

定義數組的方式一:

[root@xuexi tmp]# array_test=(1 2 3 4)

它們分別存儲在索引位0-3的位置上,是array_test[0]到array_test[3]對應的值。此時array_test[0]表示的是一個變量,所以使用$來引用。數組的引用方式:${array_name[index]}。

[root@xuexi tmp]# echo ${array_test[2]}
3

注意數組中定義是使用空格作為分隔符定義在括號內,而不是逗號。如果使用逗號,則它們將作為一個整體,也就是數組索引0的值。如果使用逗號,則:

[root@xuexi tmp]# array_test=(1,2,3,4)  

[root@xuexi tmp]# echo ${array_test[0]}   # 整體結果作為索引0位的值。
1,2,3,4 

定義數組的方式二:可以自定義索引位。

[root@xuexi tmp]# array_test1[1]=1
[root@xuexi tmp]# array_test1[2]=2
[root@xuexi tmp]# array_test1[3]=3
[root@xuexi tmp]# array_test1[4]=4
[root@xuexi tmp]# echo ${array_test1[*]}
1 2 3 4

但是在索引位4之后定義array_test1[7]=7則表示5和6的數組變量未定義,即不存在,這可以通過統計變量的元素個數來驗證。但在shell中是可以直接引用未定義變量的,只不過它們的初始值是空或是0。

(1).打印數組所有值。

[root@xuexi tmp]# echo ${array_test1[*]}
1 2 3 4

或者使用@符號。

[root@xuexi tmp]# echo ${array_test1[@]}
1 2 3 4

(2).查看數組索引號。

[root@xuexi tmp]# echo ${!array_test1[*]}
1 2 3 4

或者

[root@xuexi tmp]# echo ${!array_test1[@]}
1 2 3 4

(3).數組中變量長度和數組長度。

[root@xuexi tmp]# echo ${#array_test1[1]}  # 顯示下標為1的數組變量的字符長度

[root@xuexi tmp]# echo ${#array_test1[*]}  # 顯示數組中的元素個數(只統計值不為空的元素)
4

[root@xuexi tmp]# echo ${#array_test1[@]}  # 顯示數組中的元素個數(只統計值不為空的元素)
4

1.9.2 關聯數組

關聯數組支持字符串作為數組索引。使用關聯數組必須先使用declare -A聲明它。

[root@xuexi tmp]# declare -A array_dep   # 聲明之后就可以給其賦值了

[root@xuexi tmp]# array_dep=([name1]=longshuai [name2]=xiaofang)

其中name1和name2就是關聯數組的index。引用數組變量時需要使用index來引用對應的值。

[root@xuexi tmp]# echo ${array_dep[name1]}
longshuai

也可以分開賦值。

[root@xuexi tmp]# array_dep[name3]=zhangsan

[root@xuexi tmp]# array_dep[name4]=lisi

[root@xuexi tmp]# echo ${array_dep[name4]}
lisi

(1).查看數組所有值。

[root@xuexi tmp]# echo ${array_dep[*]}
zhangsan xiaofang longshuai lisi   # 可以看到是字母倒序排列的

或者:

[root@xuexi tmp]# echo ${array_dep[@]}
zhangsan xiaofang longshuai lisi

(2).查看數組索引號。

[root@xuexi tmp]# echo ${!array_dep[@]}   # 對應數組值的倒序排列
name3 name2 name1 name4

或者:

[root@xuexi tmp]# echo ${!array_dep[*]}
name3 name2 name1 name4

(3).統計數組長度。

[root@xuexi tmp]# echo ${#array_dep[*]}
4

或者:

[root@xuexi tmp]# echo ${#array_dep[@]}
4

1.9.3 數組元素截取、替換

和變量的截取和替換是類似的。

array=(1 2 3 4 5 6)
array0=${array[*]:2:2}   # 從數組全部元素中第2個元素向后截取2個元素出來(即3 4)
array1=${array[*]/5/6}   # 將數組中的5替換稱6

還有從左匹配刪除從右匹配刪除,和變量是一樣的。

array=(one two three foue five)

array1=${array[*]#*o}   # 從左非貪婪匹配並刪除所有數組變量中匹配內容
array2=${array[*]##*o}  # 從左貪婪匹配並刪除所有數組變量中匹配的內容
array2=${array[*]%o}    # 從右非貪婪匹配並刪除所有數組變量中匹配內容
array2=${array[*]%%o}   # 從右貪婪匹配並刪除所有數組變量中匹配內容

1.9.4 for循環遍歷數組

在shell中的循環結構中,可以使用數組名來表示整個數組變量。

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

或者讓i變成數組index的方法:

for i in ${!array[*]};do
    echo ${array[$i]}
done

以下是遍歷數組的三個常見用法總結:

array=($(ls /boot))

for i in ${array[*]};do  # 以數組值的方式直接遍歷數組
    echo $i
done

for ((i=0;i<${#array[*]};i++));do    # 以數組變量個數的方式遍歷數組
    echo ${array[$i]}
done

for i in ${!array[*]};do      # 以數組index的方式遍歷數組
    echo ${array[$i]}
done

 以下是一個數組遍歷的示例:統計文件中重復行的次數。假設a.log文件中內容如下。

[root@xuexi ~]# cat a.log
portmapper
portmapper
portmapper
portmapper
portmapper
portmapper
status
status
mountd
mountd
mountd
mountd
mountd
mountd
nfs
nfs
nfs_acl
nfs
nfs
nfs_acl
nlockmgr
nlockmgr
nlockmgr
nlockmgr
nlockmgr
nlockmgr

以下是數組遍歷的腳本。

#!/bin/bash

declare -A array_test  # 定義關聯數組
for i in `cat ~/a.log` do let ++array_test[$i] done for j in ${!array_test[*]} do printf "%-15s %3s\n" $j :${array_test[$j]} done

該腳本中第一個for循環中,以文件中內容做數組的index,每遍歷到一個index就對該index進行數學運算的自加1操作。這一過程是遍歷文件內容並為數組變量賦值的過程。

這樣就得到如下結果:

array_test[status]=2
array_test[nfs]=4
array_test[portmapper]=6
array_test[nlockmgr]=6
array_test[nfs_acl]=2
array_test[mountd]=6

第二個for循環是將array_test數組中所有數組變量及其值取出來,這一步是遍歷數組的過程。所以得到最終的結果:

[root@xuexi ~]# ./a.sh                 
status           :2
nfs              :4
portmapper       :6
nlockmgr         :6
nfs_acl          :2
mountd           :6


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM