一、Shell的数组概述
1.什么是数组
数组也是变量的一种
传统的变量只能存储一个值,但是数组可以存储多个值。
2.数组的分类
普通数组和关联数组 索引
普通数组:只能使用正整数作为数组索引
关联数组:不管可以使用整数作为索引,同时也可以使用字符串作为索引
二、普通数组的使用
1.单个赋值
[root@xian /server/scripts]# array[0]=lixian [root@xian /server/scripts]# array[1]=18 [root@xian /server/scripts]# array[2]=boy [root@xian /server/scripts]# echo ${array[*]} lixian 18 boy [root@xian /server/scripts]# declare -a |tail -1 declare -a array='([0]="lixian" [1]="18" [2]="boy")'
2. 赋值多个值
[root@xian /server/scripts]# array2=(lixian 18 'hen shuai') [root@xian /server/scripts]# echo ${array2[*]} lixian 18 hen shuai [root@xian /server/scripts]# declare -a |tail -2 declare -a array='([0]="lixian" [1]="18" [2]="boy")' declare -a array2='([0]="lixian" [1]="18" [2]="hen shuai")'
3.获取某个索引值的长度
[root@xian /server/scripts]# echo ${array[*]} lixian 18 boy [root@xian /server/scripts]# echo ${#array[0]} 6 [root@xian /server/scripts]# echo ${#array[1]} 2 [root@xian /server/scripts]# echo ${#array[2]} 3
4.获取数组中元数的个数(值的个数 或 索引的个数)
[root@xian /server/scripts]# echo ${array[*]} lixian 18 boy [root@xian /server/scripts]# echo ${#array[*]} 3 [root@xian /server/scripts]# echo ${#array[@]} 3
5.自定义索引及显示索引
[root@xian /server/scripts]# array2=(aaa bbb [6]=ccc ddd) [root@xian /server/scripts]# echo ${!array2[*]} 0 1 6 7 [root@xian /server/scripts]# echo ${array2[*]:0} aaa bbb ccc ddd [root@xian /server/scripts]# echo ${array2[*]:1} bbb ccc ddd [root@xian /server/scripts]# echo ${array2[*]:2} ccc ddd [root@xian /server/scripts]# echo ${array2[*]:1:2} bbb ccc [root@xian /server/scripts]# echo ${array2[*]:1:3} bbb ccc ddd
三、 关联数组的应用
1.单个赋值
[root@xian /server/scripts]# declare -A g_array [root@xian /server/scripts]# g_array[name]=lixian [root@xian /server/scripts]# g_array[age]=18 [root@xian /server/scripts]# g_array[skill]=linux [root@xian /server/scripts]# g_array[like]=girl [root@xian /server/scripts]# [root@xian /server/scripts]# echo ${g_array[*]} girl lixian 18 linux
2.多个赋值
[root@xian /server/scripts]# declare -A g_array2 [root@xian /server/scripts]# g_array2=([name]=xiaoxian [age]=20 [skill]=linux [like]=boy) [root@xian /server/scripts]# echo ${g_array2[*]} boy xiaoxian 20 linux
3.显示关联数组及取消单个索引值
[root@xian /server/scripts]# declare -A | tail -2 declare -A g_array='([like]="girl" [name]="lixian" [age]="18" [skill]="linux" )' declare -A g_array2='([like]="boy" [name]="xiaoxian" [age]="20" [skill]="linux" )' [root@xian /server/scripts]# unset g_array2[skill] [root@xian /server/scripts]# declare -A | tail -2 declare -A g_array='([like]="girl" [name]="lixian" [age]="18" [skill]="linux" )' declare -A g_array2='([like]="boy" [name]="xiaoxian" [age]="20" )'
四、 数组遍历与循环
案例:根据passwd文件,取出每一种shell的数量
[root@xian /server/scripts]# cat passwd.sh #!/bin/bash # File Name: passwd.sh # Author: lixian ####################### #声明关联数组 declare -A Array_Shells #批量赋值 while read line do #Type=$(echo $line | awk -F: '{print $NF}') let Array_Shells[${line##*:}]++ done < /etc/passwd #批量打印结果 for i in ${!Array_Shells[*]} do echo -e "Shell: $i\t\tCount: ${Array_Shells[$i]}" done [root@xian /server/scripts]# sh passwd.sh Shell: /sbin/nologin Count: 21 Shell: /bin/sync Count: 1 Shell: /bin/bash Count: 7 Shell: /sbin/shutdown Count: 1 Shell: /sbin/halt Count: 1