數(shù)組
- 普通數(shù)組 只能以數(shù)字作為索引(下標(biāo))
關(guān)聯(lián)數(shù)組 可以使用數(shù)字也可以使用字符串作為索引(下標(biāo))
? 數(shù)組名[索引]=值
? declare -a 查看普通數(shù)組
定義普通數(shù)組式
第一種定義方
數(shù)組名[索引]=值
[root@web scripts]# array[0]=shell
[root@web scripts]# array[1]=Linux
[root@web scripts]# array[2]=MySQL
第二種定義方式 一次定義多個值
數(shù)組名=(值)
[root@web02 ~]# array=(shell mysql [20]=kvm [50]=test)
[root@web02 ~]# echo ${array[*]}
shell mysql kvm test
[root@web02 ~]# echo ${!array[*]}
0 1 20 50
3.如何查看值
查看某個索引的值
[root@web scripts]# echo ${array[2]}
MySQL
[root@web scripts]# echo ${array[1]}
Linux
[root@web scripts]# echo ${array[0]}
shell
查看所有的值
[root@web scripts]# echo ${array[*]}
shell Linux MySQL
[root@web scripts]# echo ${array[@]}
shell Linux MySQL
如何查看索引
[root@web scripts]# echo ${!array[*]}
0 1 2
[root@web scripts]# echo ${!arraytest[*]}
0 1 2 3 4
[root@web scripts]# echo ${arraytest[*]}
shell Linux Mysql kvm docker
4.案例:
[root@web scripts]# cat array.sh
#!/bin/sh
ip=(
10.0.0.7
10.0.0.8
10.0.0.254
10.0.0.1
)
#for i in ${ip[*]}
#do
# ping -c 1 -W 1 $i
#done
for i in ${!ip[*]}
do
ping -c 1 -W 1 ${ip[$i]}
done
關(guān)聯(lián)數(shù)組 字符串作為索引
1.如何定義關(guān)聯(lián)數(shù)組
declare -A array
[root@web02 ~]# declare -A array
[root@web02 ~]# array[index1]=Shell
[root@web02 ~]# array[index2]=Linux
[root@web02 ~]# array[index3]=MySQL
[root@web02 ~]# echo ${array[index1]}
Shell
[root@web02 ~]# echo ${array[index2]}
Linux
[root@web02 ~]# echo ${array[index3]}
MySQL
[root@web02 ~]# echo ${array[*]}
Shell Linux MySQL
[root@web02 ~]# echo ${!array[*]}
index1 index2 index3
2.查看數(shù)組的長度
echo ${#array[*]}
3.遍歷數(shù)組 三種方式
1. echo ${array[*]} for循環(huán)
2. echo ${!array[*]} 使用索引遍歷內(nèi)容
3. echo ${#array[*]} 索引的個數(shù)遍歷內(nèi)容
4.數(shù)組統(tǒng)計個數(shù)
[root@web02 ~]# let array[a]++
[root@web02 ~]# let array[a]++
[root@web02 ~]# let array[a]++
[root@web02 ~]# let array[a]++
[root@web02 ~]# let a++
[root@web02 ~]# let a++
[root@web02 ~]# echo $a
2
[root@web02 ~]# let b++
[root@web02 ~]# let b++
[root@web02 ~]# echo $b
2
[root@web02 ~]# let array[b]++
[root@web02 ~]# let array[b]++
[root@web02 ~]# let array[b]++
[root@web02 ~]# echo ${array[b]}
3
5.案例一:
sex.txt
m
m
f
m
m
f
f
x
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
for i in `cat sex.txt`
do
let array[$i]++
done
for i in ${!array[*]}
do
echo "$i出現(xiàn)了 ${array[$i]}次"
done
6.案例二:
[root@web02 ~]# cat sex.txt
zs m
ls m
em f
alex m
ld m
oldboy f
bgx x
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
while read line
do
type=`echo $line|awk '{print $2}'`
let array[$type]++
done<sex.txt
for i in ${!array[*]}
do
echo "$i出現(xiàn)了 ${array[$i]}次"
done
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
while read line
do
let array[`echo $line|awk '{print $2}'`]++
done<sex.txt
for i in ${!array[*]}
do
echo "$i出現(xiàn)了 ${array[$i]}次"
done
7.案例三:統(tǒng)計Ip地址
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
while read line
do
type=`echo $line|awk '{print $1}'`
let array[$type]++
done</var/log/nginx/access.log
for i in ${!array[*]}
do
echo "$i出現(xiàn)了 ${array[$i]}次"
done