1. 程式人生 > >shell 編程數組總結

shell 編程數組總結

shell 數組結束

數組總結


目錄:

  1. 數組組成

  2. 數組賦值

  3. 數組輸出

  4. 數組案例


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 編程數組總結