標籤:shell 數組結束
數組總結
目錄:
-
數組組成
-
數組賦值
-
數組輸出
-
數組案例
1.數組組成
數組的組成就是一個元素的集合,將多個元素利用一個變數儲存,避免一個元素採用一個變數而導致形成大量的變數,數組構成由數組名(變數)、元素(變數值)和數組下標組,讀取一個數組時採用文法結構為:${變數名[索引編號]},其等價於{$變數名[1]、$變數名[2]..$變數名[n]}。
數組的分類主要分為兩類,第一類是普通數組,普通數組索引編號是連續的,申明普通數組採用文法結構:delcare –a 數組名。第二類是關聯陣列,關聯陣列是索引編號不連續的的數組,申明關聯陣列採用文法結構:declare –A 數組名。
2.數組賦值
變數的複製類似for迴圈中迴圈取值,可以通過多種方式進行賦值,具體方法如下:
方法1:通過小括弧將整數列表直接值賦值給數組
|
#!/bin/bash
declare -a arr
arr=( 1 2 3 ) #將定義整數列表賦值
for i in {0..2};do
echo "this is the $i times"
echo "value=${arr[$i]}"
done |
方法:2:通過命令傳回值進行
|
[[email protected] ~/txt]#vim array.sh
#!/bin/bash
arr=( $(ls /root/txt/) )
for i in {0..2};do
echo "this is the $i times"
echo "value=${arr[$i]}"
done
[[email protected] ~/txt]#./array.sh #執行指令碼則顯示指令碼名稱,僅僅是指令碼名稱而已
this is the 0 times
value=1.txt
this is the 1 times
value=2.txt
this is the 2 times
value=3.txt |
方法3:通過萬用字元進行賦值
|
[[email protected] ~/txt]#vim array.sh
#!/bin/bash
declare -a arr1
arr1=(/root/txt/*.txt) #數組賦值檔案,對檔案進行處理
for j in {0..2};do
echo "this is the $j times"
[ -f ${arr1[$j]} ] && echo zunzai || echo bucunzai #通過數組元素判斷檔案是否存在
lines=`cat ${arr1[$j]} |wc -l` #統計每個檔案的行號
let sum+=$lines
echo $sum
echo "value=${arr1[$j]}" #通過數組顯示檔案名稱
done
[[email protected] ~/txt]#./array.sh #驗證執行結果
this is the 0 times
this is first txt.word
value=1.txt
this is the 1 times
this is sencond txt.word
value=2.txt
this is the 2 times
this is three txt.word
value=3.txt |
3.數組輸出
數組相當於一系列的變數元素的集合,在數組輸出時,可以輸出指定的元素、輸出整體的元素和元素的個數:
1.輸出整體的元素,採用文法結構為${變數名[*|@]},其中*|@表示萬用字元任意的意思,因此會輸出所有的元素。
|
[[email protected] ~/txt]#intarr=( 1 2 3 )
[[email protected] ~/txt]#echo ${intarr[*]}
1 2 3 |
2.輸出指定的元素,採用文法結構為${變數名[索引編號]},其中索引編號從0開始
|
[[email protected] ~/txt]#intarr=( 1 2 3 )
[[email protected] ~/txt]#echo ${intarr[0]}
1
[[email protected] ~/txt]#echo ${intarr[1]}
2 |
3.在數組中修改其中某一個陣列變數的元素或增加一個元素,採用文法結構為:變數名[索引編號]=***,當變數的索引編號存在時,覆蓋變數元素原有的值,如若不存在變數的索引編號時,在數組中添加新增的變數元素。
|
[[email protected] ~/txt]#intarr[1]=20 #存在索引編號1,因此覆蓋原有值
[[email protected] ~/txt]#echo ${intarr[1]}
20
[[email protected] ~/txt]#intarr[3]=40 #不存在索引編號3,因此新增變數值
[[email protected] ~/txt]#echo ${intarr[*]}
1 20 3 40 |
4.在數組中將某個元素刪除採用文法結構為:unset 數組[索引編號],刪除整個數組時:unset數組。
|
[[email protected] ~/txt]#echo ${intarr[*]} #列印真箇數組
1 20 3 40
[[email protected] ~/txt]#unset intarr[3] #刪除數組中第4個元素
[[email protected] ~/txt]#echo ${intarr[*]}
1 20 3
[[email protected] ~/txt]#unset intarr #刪除真箇數組
[[email protected] ~/txt]#echo ${intarr[*]} |
5.在數組中將截取某一段數組元素,文法結構分別為:${數組名[@]:offset:#},offset表示數組位移個數,#表示取位移後的多少個元素。
|
[[email protected] ~/txt]#intarr=({a..z}) #產生序列
[[email protected] ~/txt]#echo ${intarr[*]}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[[email protected] ~/txt]#echo ${intarr[@]:2:2} #取第2個元素後面的兩個元素
c d |
6.在數組中將數組某元素替換,文法結構分別為:${數組名[@]/###/***},offset表示數組中元素為###的替換為***,注意替換的是元素。
|
[[email protected] ~/txt]#echo ${intarr[*]}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[[email protected] ~/txt]#echo ${intarr[@]/a/b} #匹配數組元素中的a進行替換為b
b b c d e f g h i j k l m n o p q r s t u v w x y z
|
4.數組案例
1.迴圈列印數組元素,數組包含IP地址:192.168.1.110.1.1.1 172.16.0.1;
|
#!/bin/bash
declare -a ip_arr
declare -i i=0
declare -i j
ip_arr=( #定義數組,數組元素為IP地址,充分說明數組就是變數的集合
192.168.1.1
10.1.1.1
172.16.0.1
)
###############################################
echo “方法1迴圈列印數組元素”
for i in $(seq 0 $[${#ip_arr[*]}-1]);do
echo ${ip_arr[$i]}
let i++
done
###############################################
echo "方法2迴圈列印數組元素"
for (( j=0;j<${#ip_arr[*]};j++))
do
echo ${ip_arr[$j]}
done
###################將數組作為for迴圈中的元素進行迴圈##########################
echo "方法3數組遍曆列印數組元素"
for n in ${ip_arr[*]};do
echo $n
done |
本文出自 “11831715” 部落格,請務必保留此出處http://11841715.blog.51cto.com/11831715/1962177
shell 編程數組總結