Shell数组函数:数组(二)

关联数组

        注意:先声明关联数组

        一、定义关联数组

                方法一

#一次赋一值
#数组名[索引]=变量值
[root@localhost ~]# declare -A ass_array1
[root@localhost ~]# ass_array1[index1]=pear
[root@localhost ~]# ass_array1[index2]=apple
[root@localhost ~]# ass_array1[index3]=orange
[root@localhost ~]# ass_array1[index4]=peach

[root@localhost ~]# echo ${ass_array1[*]}    #查看
peach pear apple orange

 

                方法二

#一次赋多个值
[root@localhost ~]# declare -A ass_array2
[root@localhost ~]# ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]="bash shell")

[root@localhost ~]# echo ${ass_array2[*]}    #查看
bash shell tom jack alice
[root@localhost ~]# echo ${!ass_array2[*]}
index4 index1 index2 index3
[root@localhost ~]# echo ${#ass_array2[*]}
4

 

        二、查看数组

[root@localhost ~]# declare -A
declare -A ass_array1='([index4]="peach" [index1]="pear" [index2]="apple" [index3]="orange" )'
declare -A ass_array2='([index4]="bash shell" [index1]="tom" [index2]="jack" [index3]="alice" )'

 

        三、访问数组元素

[root@localhost ~]# echo ${ass_array2[index2]}    #访问数组中的第二个元素
[root@localhost ~]# echo ${ass_array2[@]}
[root@localhost ~]# echo ${ass_array2[*]}    #访问数组中所有元素
[root@localhost ~]# echo ${!ass_array2[@]}    #获得数组元素的个数
[root@localhost ~]# echo ${#ass_array2[@]}    #获得数组元素的索引

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值