普通数组:只能使用整数作为数组索引
关联数组:可以使用字符串作为数组索引
数组名[索引]=变量值
示例
# array1[0]=pear
# array1[1]=apple
# array1[2]=orange
# array1[3]=peach
数组名=(变量值1 变量值2 变量值3 ...)
示例
# books=(linux shell awk openstack docker)
--------------------------------------------
| linux | shell | awk | openstack | docker |
--------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 索引(下标)
--------------------------------------------
# array2=(tom jack alice)
# array3=(`cat /etc/passwd`) 希望是将该文件中的每一个行作为一个元数赋值给数组 array3(由于分隔符的原因,最终可能无法满足预期)
# array4=(`ls /var/ftp/Shell/for*`)
# array5=(tom jack alice "bash shell")
# colors=($red $blue $green $recolor)
# array5=(1 2 3 4 5 6 7 "linux shell" [20]=saltstack)
${数组名[索引]}
示例
# 定义普通数组
[root@hadoop04 ~]# books=(linux shell awk openstack docker)
# 访问普通数组
[root@hadoop04 ~]# echo ${books[0]}
linux
[root@hadoop04 ~]# echo ${books[1]}
shell
[root@hadoop04 ~]# echo ${books[2]}
awk
[root@hadoop04 ~]# echo ${books[3]}
openstack
[root@hadoop04 ~]# echo ${books[4]}
docker
${数组名[@]}
# 等同于 ${数组名[*]}
示例
[root@hadoop04 ~]# echo ${books[@]}
linux shell awk openstack docker
[root@hadoop04 ~]# echo ${books[*]}
linux shell awk openstack docker
${#数组名[@]}
示例
[root@hadoop04 ~]# echo ${#books[@]}
5
${!数组名[@]}
示例
[root@hadoop04 ~]# echo ${!books[@]}
0 1 2 3 4
从索引 n 开始访问数组元数
${array1[@]:n}
示例
[root@hadoop04 ~]# echo ${books[@]:3}
openstack docker
从索引 n 开始访问,访问m个元数
${array1[@]:n:m}
示例
[root@hadoop04 ~]# echo ${books[@]:2:2}
awk openstack
通过数组元数的索引进行遍历
示例
[root@hadoop04 shell_array]# vim array_hosts_while.sh
#!/urs/bin/bash
# 定义数组
while read line
do
hosts[i++]=$line
done < /etc/hosts
echo "hosts first: ${hosts[0]}"
echo
# 遍历数组
for i in ${!hosts[@]}
do
echo "hosts${i}: ${hosts[${i}]}"
done
[root@hadoop04 shell_array]# vim array_hosts_for.sh
#!/usr/bin/bash
# for array
# 定义分隔符为回车
OLD_IFS=${IFS}
IFS=$'\n'
# 定义数组
for line in `cat /etc/hosts`
do
hosts[j++]=$line
done
# 遍历数组
for i in ${!hosts[@]}
do
echo "hosts${i}: ${hosts[${i}]}"
done
IFS=${OLD_IFS}
声明关联数组变量
declare -A 数组名
示例
# declare -A ass_array1
# declare -A ass_array2
数组名[索引]=变量值
示例
# 声明关联数组
[root@hadoop04 shell_array]# declare -A ass_array1
# 定义关联数组
[root@hadoop04 shell_array]# ass_array1[index1]=pear
[root@hadoop04 shell_array]# ass_array1[index2]=apple
[root@hadoop04 shell_array]# ass_array1[index3]=orange
[root@hadoop04 shell_array]# ass_array1[index4]=peach
# 查看关联数组
[root@hadoop04 shell_array]# declare -A | grep ass_array1
declare -A ass_array1='([index4]="peach" [index1]="pear" [index2]="apple" [index3]="orange" )'
数组名=([索引1]=变量值1 [索引2]=变量值2 [索引3]=变量值3 ...)
示例
# 声明关联数组
[root@hadoop04 ~]# declare -A person_info
# 定义关联数组
[root@hadoop04 ~]# person_info=([name]=ken [sex]=male [age]=27 [skill]=cloud)
# 访问关联数组
[root@hadoop04 ~]# echo ${person_info[age]}
27
${数组名[索引]}
示例
# 声明关联数组
[root@hadoop04 ~]# declare -A person_info
# 定义关联数组
[root@hadoop04 ~]# person_info=([name]=ken [sex]=male [age]=27 [skill]=cloud)
# 访问索引为age的元数
[root@hadoop04 ~]# echo ${person_info[age]}
27
${数组名[@]}
# 等同于 ${数组名[*]}
示例
[root@hadoop04 ~]# echo ${person_info[@]}
ken 27 cloud male
[root@hadoop04 ~]# echo ${person_info[*]}
ken 27 cloud male
${#数组名[@]}
示例
[root@hadoop04 ~]# echo ${#person_info[@]}
4
${!数组名[@]}
示例
[root@hadoop04 ~]# echo ${!person_info[@]}
name age skill sex
通过数组元数的索引进行遍历
declare -a
declare -A
☆☆☆ 把要统计的对象作为数组的索引
[root@hadoop04 shell_array]# vim sex.txt
jack m
alice f
tom m
rose f
robin m
zhuzhu f
[root@hadoop04 shell_array]# vim count_sex.sh
#!/usr/bin/bash
# count sex
# v1.0 by ElegantSmile
# 定义关联数组
declare -A sex
while read line
do
type=`echo ${line} | awk '{print $2}'`
# 把要统计的对象作为数组的索引,通过自增实现统计
let sex[${type}]++
done < sex.txt
for i in ${!sex[@]}
do
echo "sex $i: ${sex[$i]}"
done
[root@hadoop04 shell_array]# bash count_sex.sh
sex f: 3
sex m: 3
使用关联数组统计文件 /etc/passwd 中用户使用的不同类型 shell 的数量
[root@hadoop04 shell_array]# vim count_shells.sh
#!/usr/bin/bash
# count shells
# v1.0 by ElegantSmile
# 定义关联数组
declare -A shells
while read line
do
type=`echo ${line} | awk -F ':' '{print $NF}'`
let shells[${type}]++
done < /etc/passwd
for i in ${!shells[@]}
do
echo "shell $i : ${shells[${i}]}"
done
# 执行脚本
[root@hadoop04 shell_array]# bash count_shells.sh
shell /sbin/nologin : 17
shell /bin/sync : 1
shell /bin/bash : 3
shell /sbin/shutdown : 1
shell /sbin/halt : 1
统计tcp不同连接状态的数量
[root@hadoop04 shell_array]# vim count_tcpconn_status.sh
#!/usr/bin/bash
# count tcp status
# v1.0 by ElegantSmile
declare -A status
# 注意centos6和centos7上ss -an的显示效果顺序不同
type=`ss -an | grep :80 | awk '{print $2}'`
for i in ${type}
do
let status[${i}]++
done
for i in ${!status[@]}
do
echo "status $i : ${status[${i}]}"
done
手机扫一扫
移动阅读更方便
你可能感兴趣的文章