數組
一 數組介紹
什麼是數組?
數組就是一系列元素的集合,一個數組内可以存放多個元素
為何要用數組?
我們可以用數組将多個元素彙總到一起,避免單獨定義的麻煩
二 數組的使用
2.1 數組的定義
# 方式一:array=(元素1 元素2 元素3)
array=(egon 18 male)
# 方式二:array=([key1]=value1 [key2]=value2 [key3]=value3)
array=([0]=111 [1]="two" [2]=333)
# 方式三:依次指派
array_new[0]=111
array_new[1]=222
array_new[2]="third"
# 方式四:利用執行指令的結果設定數組元素:array=($(指令)) 或者 array=(`指令`)
該方式會将指令的結果以空格為分隔符切成多個元素然後指派給數組
[[email protected] ~]# ls /test
a.txt b.txt
[[email protected] ~]# array3=(`ls /test`)
[[email protected] ~]# declare -a |grep array3
declare -a array3='([0]="a.txt" [1]="b.txt")'
# ps:檢視聲明過的數組
declare -a
2.2 通路數組内元素
[[email protected] ~]# array=(egon 18 male)
#1、按照索引通路數組内指定位置的元素
[[email protected] ~]# echo ${array[0]}
egon
[[email protected] ~]# echo ${array[1]}
18
[[email protected] ~]# echo ${array[2]}
male
[[email protected] ~]# echo ${array[-1]} # 支援負向索引
male
# 2、通路數組内全部元素資訊
[[email protected] ~]# echo ${array[*]}
egon 18 male
[[email protected] ~]# echo ${array[@]}
egon 18 male
# 3、擷取數組元素的長度
[[email protected] ~]# echo ${#array[*]}
3
[[email protected] ~]# echo ${#array[@]}
3
2.3 修改/添加數組元素
[[email protected] ~]# array=(egon 18 male)
[[email protected] ~]# array[0]="EGON" # 修改
[[email protected] ~]# array[3]="IT" # 添加
[[email protected] ~]# declare -a |grep array
declare -a array='([0]="EGON" [1]="18" [2]="male" [3]="IT")'
2.4 删除數組元素
[[email protected] ~]# unset array[0]
[[email protected] ~]# echo ${array[*]}
18 male IT
[[email protected] ~]# declare -a |grep array
declare -a array='([1]="18" [2]="male" [3]="IT")'
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# unset array[2]
[[email protected] ~]# declare -a |grep array
declare -a array='([1]="18" [3]="IT")'
[[email protected] ~]# unset array # 删除整個數組
[[email protected] ~]# echo ${array[*]}
[[email protected] ~]#
2.5 數組内元素的截取
[[email protected] ~]# array=(egon 18 male IT 1.80)
[[email protected] ~]# echo ${array[*]:1} # 從索引1開始,一直到最後
18 male IT 1.80
[[email protected] ~]# echo ${array[*]:1:3} # 從索引1開始,通路3個元素
18 male IT
[[email protected] ~]# array=(one two three four five fire)
[[email protected] ~]# echo ${array[*]#one}
two three four five fire
[[email protected] ~]# echo ${array[*]#f*e}
one two three four
2.6 數組内容的替換
[[email protected] ~]# array=(one two three four five fire)
[[email protected] ~]# echo ${array[*]/five/abc}
one two three four abc fire
[[email protected] ~]# echo ${array[*]/f*e/abc}
one two three four abc abc
三 關聯數組
數組分為兩種
- 普通數組:隻能使用整數作為數組索引,我們前面介紹的都是普通數組
- 關聯數組:可以使用字元串作為數組索引,需要用declare -A聲明
聲明關聯數組
[[email protected] ~]# declare -A info
[[email protected] ~]# info["name"]="egon"
[[email protected] ~]# info["age"]=18
[[email protected] ~]# info["gender"]="male"
[[email protected] ~]#
[[email protected] ~]# declare -A |grep info
declare -A info='([gender]="male" [name]="egon" [age]="18" )'
[[email protected] ~]#
[[email protected] ~]# echo ${info[*]}
male egon 18
[[email protected] ~]#
[[email protected] ~]# echo ${info["name"]}
egon
四 周遊數組
方法一: 利用擷取所有資訊進行周遊 (适用于普通數組與關聯數組)
# 例1
declare -A array
array=(["name"]="egon" ["age"]=18 ["gender"]="male")
for item in ${array[*]}
do
echo $item
done
# 例2
array=("egon" 18 "male")
for item in ${array[*]}
do
echo $item
done
方法二: 通過數組元數的索引進行周遊(适用于普通數組與關聯數組)
# 例1
declare -A array
array=(["name"]="egon" ["age"]=18 ["gender"]="male")
for i in ${!array[*]} # echo ${!array[*]} # 擷取的是key資訊:name age gender
do
echo "$i:${array[$i]}"
done
# 例2
array=("egon" 18 "male")
for i in ${!array[*]} # echo ${!array[*]} 直接擷取所有元素的索引資訊
do
echo $i
echo ${array[i]}
done
方法三:根據數組長度資訊進行周遊,(适用于普通數組)
array=("egon" 18 "male")
for ((i=0;i<${#array[*]};i++))
do
echo "$i:${array[$i]}"
done
五 練習
練習1:對指定的IP位址進行網絡測試
#!/bin/bash
array=(
10.0.0.7
10.0.0.8
10.0.0.9
10.0.0.41
)
for info in ${array[*]}
do
ping -c 2 -W 1 $info
done
練習2: 統計登入shell的種類及對應的個數(關聯數組)
#!/bin/bash
declare -A array_for_shell
while read line # done後面接<将檔案重定向給while;while後再接read将檔案流指派給變量
do
login_shell=`echo $line | cut -d: -f7`
let array_for_shell["$login_shell"]++ # 當使用let時,變量前面不必加上$
done < /etc/passwd
for k in ${!array_for_shell[*]}
do
echo $k:${array_for_shell[$k]}
done
練習3:擷取檔案指定列的資訊
[[email protected] test]# cat a.sh
#!/usr/bin/env bash
declare -A array
while read line
do
let array[`echo $line | cut -d: -f7`]++
done < /etc/passwd
for k in ${!array[*]}
do
echo $k:${array[$k]}
done
[[email protected] test]# ./a.sh
/sbin/nologin:41
/bin/sync:1
/bin/bash:2
/sbin/shutdown:1
/sbin/halt:1