[TOC]
shell数组
01. shell数组的分类
- 普通数组:只能以数字作为索引(也称之为下标)
- 关联数组:可以使用字符作为索引
02. shell数组常用定义方式
定义普通数组
#定义普通数组1:
[root@shell ~]# array=(one two three)
#定义普通数组2:
[root@shell ~]# array=([0]=one [1]=two [2]=three)
#定义普通数组3:
[root@shell ~]# array[0]=one
[root@shell ~]# array[1]=two
[root@shell ~]# array[2]=three
#不连续的索引赋值,双引号将里面的内容当成一个元素
[root@shell ~]# array_1=(oldboy name [5]=linux "centos shell" [10]=kvm)
[root@shell ~]# echo ${array_1[*]}
oldboy name linux centos shell kvm
[root@shell ~]# echo ${!array_1[*]}
0 1 5 6 10
定义管理数组
创建关联数组前需要声明
#定义关联数组1:
[root@shell ~]# declare -A oldqq
[root@shell ~]# oldqq=([index1]=www [index2]=bbs [index3]=zhihu)
#定义关联数组2:
[root@shell ~]# declare -A oldqq
[root@shell ~]# oldqq[index1]=www
[root@shell ~]# oldqq[index2]=bbs
[root@shell ~]# oldqq[index3]=zhihu
03. 查看shell数组
查看所有定义的普通数组
[root@shell ~]# declare -a
查看所有定义的关联数组
[root@shell ~]# declare -A
查看数组所有元素(*与@都可以)
[root@shell ~]# echo ${array[*]}
one two three
[root@shell ~]# echo ${oldqq[@]}
index1 index2 index3
查看数组所有的索引(下标)
[root@shell ~]# echo ${!array[*]}
0 1 2
[root@shell ~]# echo ${oldqq[*]}
www bbs zhihu
查看数组的单个元素
[root@shell ~]# echo ${array[0]}
one
[root@shell ~]# echo ${array[2]}
three
[root@shell ~]# echo ${oldqq[index1]}
www
[root@shell ~]# echo ${oldqq[index3]}
zhihu
04. 删除shell数组
删除单个数组元素
[root@shell ~]# unset array[3] #<= 去掉下标为3的数组元素
[root@shell ~]# echo ${array[*]}
one two three
删除整个数组
[root@shell ~]# unset array #<= 删除整个数组
[root@shell ~]# echo ${array[*]} #<= 内容为空,说明删除成功
05. shell数组内容的截取和替换
[root@shell ~]# array=(a b c d e)
[root@shell ~]# echo ${!array[*]}
0 1 2 3 4
[root@shell ~]# echo ${array[*]}
a b c d e
[root@shell ~]# echo ${array[*]:1:3} #<== 截取元素1到元素3(默认元素从0开始)
b c d
[root@shell ~]# array=($(echo {a..g})) #<== 将变量的结果赋值给数组元素
[root@shell ~]# echo ${array[*]}
a b c d e f g
[root@shell ~]# echo ${array[*]/a/1} #<== 把数组中a替换为1,原数组未被真正修改
1 b c d e f g
#数组也是变量,因此也适合变量的字串处理功能
[root@shell ~]# array=(abcABCabc abcdefg)
[root@shell ~]# echo ${array[*]}
abcABCabc abcdefg
[root@shell ~]# echo ${array[*]#*c}
ABCabc defg
[root@shell ~]# echo ${array[*]#*b}
cABCabc cdefg
[root@shell ~]# echo ${array[*]##*b}
c cdefg
[root@shell ~]# echo ${array[*]%b*}
abcABCa a
[root@shell ~]#
[root@shell ~]# echo ${array[*]%%b*}
a a
06. shell数组循环打印
#通过C语言类型的for循环
[root@shell scripts]# cat test01sh
#!/bin/bash
ipadd=(
10.0.0.11
10.0.0.22
10.0.0.33
)
for((i=0;i<${#ipadd[*]};i++))
do
echo "${ipadd[$i]}"
done
#通过普通for循环
[root@shell scripts]# cat test02sh
#!/bin/bash
ipadd=(
10.0.0.11
10.0.0.22
10.0.0.33
)
for i in ${ipadd[*]}
do
echo "$i"
done
#两种打印结果是相同的,这里省略..
[root@shell scripts]# sh test01h
10.0.0.11
10.0.0.22
10.0.0.33
07. 案例:
计算以下数据中性别出现的次数
[root@shell ~]# cat sex.txt |column -t
zhangsan male
lisi female
wangwu male
lili male
xx female
mayun male
mahuateng male
wangjianlin male
cxk unknown
编写的脚本
[root@shell ~]# cat sex.sh
#!/bin/bash
for i in `awk '{print $2}' /root/sex.txt`
do
declare -A sex
let sex[$i]++
done
for a in ${!sex[*]}
do
echo "性别:$a 出现了:${sex[$a]}"
done
#脚本执行结果
[root@shell ~]# sh sex.sh |column -t
性别:male 出现了:6
性别:unknown 出现了:1
性别:female 出现了:2