linux shell -----数组和关联数组

数组允许脚本利用索引将数集合保存为独立的条目。Bash支持普通数组和关联上数组

  • 普通数组使用整数作为数据索引
  • 关联数组使用字符串作为数组索引

数组

定义数组

  • 使用数值列定义
[root@test255150 ~]# arr=(t1 t2 3)
[root@test255150 ~]#

这些值将会存储在以0位其实索引的连续位置上
- 使用’索引-值’定义

[root@test255150 ~]# arrKeyVal[0]='kv1'
[root@test255150 ~]#

数组操作

  • 打印特定索引的数组元素
[root@test255150 ~]# echo ${arr}
t1
[root@test255150 ~]# echo ${arr[0]}
t1
[root@test255150 ~]# echo ${arr[1]}
t2
[root@test255150 ~]# echo ${arr[2]}
3
[root@test255150 ~]# arrKeyVal[1]='kv2'
[root@test255150 ~]# echo ${arrKeyVal[1]}
kv2
[root@test255150 ~]#
  • 以列表打印出数组中的所有值
[root@test255150 ~]# echo ${arr[*]}
t1 t2 3
[root@test255150 ~]# echo ${arr[@]}
t1 t2 3
[root@test255150 ~]#
  • 打印数组长度
[root@test255150 ~]# echo ${#arr[@]}
3
[root@test255150 ~]# echo ${#arr[*]}
3
[root@test255150 ~]# 
  • 列出数组索引
[root@test255150 ~]# echo ${!arr[*]}
0 1 2
[root@test255150 ~]# echo ${!arr[@]}
0 1 2
[root@test255150 ~]# 

总结:
echo ${!array[*]} #取关联数组所有键
echo ${!array[@]} #取关联数组所有键
echo ${array[*]} #取关联数组所有值
echo ${array[@]} #取关联数组所有值
echo ${#array[*]} #取关联数组长度
echo ${#array[@]} #取关联数组长度

关联数组

使用关联数组的前提:Bash 4.0+,因为从bash 4.0才支持关联数组

在关联数组中我们可以使用任意文本作为数组的索引,首先声明语句将一个变量定义为关联数组:

declare -A ass_array_name

使用行内索引-值

[root@pt255142 ~]#declare -A assArr_cp
[root@pt255142 ~]#assArr_cp=(["first"]="a" ["second"]="b")
[root@pt255142 ~]#

使用独立的索引-值

[root@pt255142 ~]declare -A assArr
[root@pt255142 ~]assArr["first"]="a"
[root@pt255142 ~]assArr["second"]="b"
[root@pt255142 ~]

使用索引输出或者使用declare命令查看

[root@pt255142 ~]# declare|grep assArr
assArr=([second]="b" [first]="a" )
assArr_cp=([second]="b" [first]="a" )
[root@pt255142 ~]# 

demo

root@pt255142 ~]# declare -A assArr
root@pt255142 ~]# assArr=([second]="b" [first]="a" [third]="c" [forth]="d")
[root@pt255142 ~]# declare|grep assArr
assArr=([third]="c" [second]="b" [first]="a" [forth]="d" )
[root@pt255142 ~]# 

获取元素:获取元素:用${数组名[下标]} 得到数组元素(下标从0开始), 下标为*或者@得到整个数组内容

[root@pt255142 ~]# echo ${assArr[first]}
a
[root@pt255142 ~]# echo ${assArr[*]}
c b a d
[root@pt255142 ~]# 

赋值:通过数组名[下标]可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素

[root@pt255142 ~]# assArr[fifth]="e"
[root@pt255142 ~]# declare|grep assArr
assArr=([third]="c" [fifth]="e" [second]="b" [first]="a" [forth]="d" )
[root@pt255142 ~]# 

获取某范围的元素:直接通过 ${数组名[@或*]:起始位置:长度} 获取数组给定范围内元素,返回字符串,中间用空格分开

[root@pt255142 ~]# echo ${assArr[*]:1:2}
c e
[root@pt255142 ~]# 

替换:${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容

[root@pt255142 ~]# echo ${assArr[*]}
c e b a d
[root@pt255142 ~]# echo ${assArr[*]/a/g}
c e b g d
[root@pt255142 ~]#

删除:通过unset数组[下标]可以清除相应的元素,不带下标则清除全部数据

[root@pt255142 ~]# declare|grep assArr
assArr=([third]="c" [fifth]="e" [second]="b" [first]="a" [forth]="d" )
[root@pt255142 ~]# unset assArr["first"]
[root@pt255142 ~]# declare|grep assArr
assArr=([third]="c" [fifth]="e" [second]="b" [forth]="d" )
[root@pt255142 ~]# 

遍历所有的key和value

[root@pt255142 ~]# for key in ${!assArr[*]}
> do
> echo "${key} come from ${assArr[$key]}"
> done
third come from c
fifth come from e
second come from b
forth come from d
[root@pt255142 ~]# 

猜你喜欢

转载自blog.csdn.net/yue530tomtom/article/details/81480540