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[@]}    #获得数组元素的索引

 

 

 

 

 

相关推荐

  1. shell 函数数组

    2023-12-06 09:50:01       33 阅读
  2. Linux Shell 009-数组

    2023-12-06 09:50:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 09:50:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 09:50:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 09:50:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 09:50:01       20 阅读

热门阅读

  1. cuda 在 torch神经网络中哪些地方可以用?

    2023-12-06 09:50:01       39 阅读
  2. pytorch+huggingface+bert实现一个文本分类

    2023-12-06 09:50:01       32 阅读
  3. Pytorch指定device(cuda or cpu)

    2023-12-06 09:50:01       28 阅读