shell的数组

数组

数组的分类

 普通数组: 只能使用整数作为数组索引
 关联数组: 可以使用字符作为数组索引
 元素=索引=下标
普通数组定义
[root@shell ~]# array[0]=shell
[root@shell ~]# array[10]=MySQL
[root@shell ~]# echo ${array[0]}
shell
[root@shell ~]# 
[root@shell ~]# echo ${array[10]}
MySQL
-----------------
[root@shell ~]# array=(shell MySQL Redis)
[root@shell ~]# echo ${array[0]}
shell
[root@shell ~]# echo ${array[1]}
MySQL
[root@shell ~]# echo ${array[2]}
Redis
----------------
[root@shell ~]# array=(Shell [5]=Docker Redis [10]=MySQL)
[root@shell ~]# echo ${array[0]}
Shell
[root@shell ~]# echo ${array[5]}
Docker
[root@shell ~]# echo ${array[6]}
Redis
[root@shell ~]# echo ${array[10]}
MySQL

---------------命令定义数组
[root@shell ~]# array=(`ls`)
[root@shell ~]# echo ${array[0]}
1.sh
[root@shell ~]# echo ${array[1]}
1.txt
[root@shell ~]# echo ${array[2]}
2.sh
[root@shell ~]# echo ${array[3]}
all.sh


常用的定义方式
array=(10.0.0.8 10.0.0.9 10.0.0.32)


取消定义数组
unset array 


如何查看数组
[root@shell ~]# echo ${array[0]}       # 使用索引查看
[root@shell ~]# echo ${array[*]}       # 查看数组中所有的值
shell MySQL Redis

[root@shell ~]# echo ${array[@]}       # 查看数组中所有的值
shell MySQL Redis
    
[root@shell ~]# echo ${!array[*]}       # 查看索引
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

案例:

#!/bin/sh
#定义数组
ip=(
10.0.0.1
10.0.0.2
10.0.0.7
10.0.0.8
10.0.0.9
)
#调用数组
for i in ${ip[*]}
do
      ping -c1 -W1 $i &>/dev/null
      [ $? -eq 0 ] && echo "ping $i 没问题....." || echo "ping $i 不通....."
done

遍历索引

#!/bin/sh
#定义数组
ip=(
10.0.0.1   0
10.0.0.2   1
10.0.0.7   2
10.0.0.8   3
10.0.0.9   4
)
#调用数组
for i in ${!ip[*]}
do
      ping -c1 -W1 ${ip[$i]} &>/dev/null
      [ $? -eq 0 ] && echo "ping ${ip[$i]} 没问题....." || echo "ping ${ip[$i]} 不通....."
done

关联数组 索引可以是字符串

[root@shell day5]# declare -A array
[root@shell day5]# array[index1]=shell
[root@shell day5]# array[index2]=mysql   
[root@shell day5]# array[index3]=redis      
[root@shell day5]# 
[root@shell day5]# echo ${array[index1]}
shell
[root@shell day5]# echo ${array[index2]}
mysql
[root@shell day5]# echo ${array[index3]}
redis
[root@shell day5]# echo ${array[*]}     
shell mysql redis
[root@shell day5]# echo ${array[@]}
shell mysql redis
[root@shell day5]# echo ${!array[@]}
index1 index2 index3
[root@shell day5]# array=([index1]=shell  [test]=Redis [index2]=Redis)
[root@shell day5]# echo ${array[*]}
shell Redis Redis


查看所有的当前shell定义的数组
declare -A 

案例:
cat sex.txt
zs m
ls m
zp f
lw f
qq m
ww x

#!/bin/sh
#定义数组
declare -A array
while read sex
do
        let array[$sex]++
done<sex.txt
for i in ${!array[*]} 
do
        echo "性别$i 出现了 ${array[$i]} 次"
done
----------------
[root@shell day5]# sh array.sh
性别f 出现了 2 次
性别m 出现了 3 次
性别x 出现了 1
[root@shell day5]# declare -A array
[root@shell day5]# 
[root@shell day5]# array[m]=1
[root@shell day5]# echo ${array[m]}
1
[root@shell day5]# let array[a]++
[root@shell day5]# let array[a]++
[root@shell day5]# echo ${array[a]}
2
[root@shell day5]# let array[b]++  
[root@shell day5]# let array[b]++
[root@shell day5]# let array[b]++
[root@shell day5]# echo ${array[b]}
3
[root@shell day5]# for i in ${!array[*]};do echo $i;done
a
b
m

统计/etc/passwd 解释器出现的次数

#!/bin/sh
declare -A bash
while read line
do
     let bash[`echo $line|awk -F: '{print $NF}'`]++

done</etc/passwd
for i in ${!bash[*]}
do
        echo "$i 出现了 ${bash[$i]}次"
done

统计nginx日志ip出现的次数

#!/bin/sh
declare -A IP
while read line
do
     let IP[`echo $line|awk '{print $1}'`]++

done</var/log/nginx/access.log
for i in ${!IP[*]}
do
        echo "$i 出现了 ${IP[$i]}次"
done

猜你喜欢

转载自www.cnblogs.com/youhongliang/p/12706665.html