linux shell中 创建数组及数组的基本操作

 

001、 创建数组

a、方法1

[root@pc1 test01]# ay1=(100 200 "abc" "xyz")     ## 创建数组

 

b、方法2

[root@pc1 test01]# ay2[0]=100
[root@pc1 test01]# ay2[1]=200
[root@pc1 test01]# ay2[3]="mn"

 

c、方法3

[root@pc1 test]# list1="100 200 'abc' 'xyz'"    ## 创建一个字符串列表
[root@pc1 test]# ay1=($list1)                   ## 生成数组
[root@pc1 test]# echo ${ay1[*]}                 ## 访问数组的全部元素
100 200 'abc' 'xyz'

 

002、访问数组的全部元素

[root@pc1 test01]# ay1=(100 200 "abc" "xyz")     ## 定义数组
[root@pc1 test01]# echo ${ay1[*]}                ## 访问数组的全部元素
100 200 abc xyz

 

003、访问数组的单个元素

[root@pc1 test01]# ay1=(100 200 300 "abc")      ## 定义数组
[root@pc1 test01]# echo ${ay1[0]}               ## 普通数组访问单个元素,直接使用数字的索引
100
[root@pc1 test01]# echo ${ay1[1]}
200
[root@pc1 test01]# echo ${ay1[3]}
abc
[root@pc1 test01]# echo ${ay1[6]}               ## 索引越界,返回为空

 

004、获取数组的长度

复制代码
[root@pc1 test]# ay1[0]=100
[root@pc1 test]# ay1[1]=200                ## 定义数组
[root@pc1 test]# echo ${#ay1[*]}           ## 获取数组的长度
2
[root@pc1 test]# ay2[0]="777"
[root@pc1 test]# ay2[1]="888"
[root@pc1 test]# ay2[2]="999"
[root@pc1 test]# ay2[3]="abcd"             ## 定义数组
[root@pc1 test]# echo ${#ay2[*]}           ## 获取数组的长度
4
复制代码

 

005、获取所有数组元素的下标

[root@pc1 test]# ay1[0]=100
[root@pc1 test]# ay1[1]=200
[root@pc1 test]# ay1[2]=300
[root@pc1 test]# ay1[3]="abcd"               ## 创建数组
[root@pc1 test]# echo ${!ay1[*]}             ## 获取所有数组元素的下标
0 1 2 3

 

006、修改、删除数组中的某个元素、删除整个数组

a、修改数组中某个元素

[root@pc1 test]# ay1=(100 200 300 "ab" "xy")        ## 定义数组 
[root@pc1 test]# echo ${ay1[*]}                     ## 输出数组的所有元素
100 200 300 ab xy
[root@pc1 test]# ay1[1]=88888                       ## 修改数组的某一元素
[root@pc1 test]# echo ${ay1[*]}                     ## 确认修改效果
100 88888 300 ab xy

 

b、删除数组中某一个元素

复制代码
[root@pc1 test]# ay1=(100 200 500 800 "abcd")   ## 定义了一个长度为5的数组
[root@pc1 test]# echo ${ay1[*]}                 ## 输出数组的所有元素
100 200 500 800 abcd
[root@pc1 test]# unset ay1[2]                   ## 删除数组的第三个元素
[root@pc1 test]# echo ${ay1[*]}                 ## 确认删除效果
100 200 800 abcd
[root@pc1 test]# echo ${!ay1[*]}                ## 返回数组的所有的下标
0 1 3 4
[root@pc1 test]# echo ${ay1[2]}                 ## 输出删除的元素的值,为空

[root@pc1 test]# echo ${ay1[3]}                 ## 其它元素未发生变化
800
复制代码

 

c、删除整个数组

[root@pc1 test]# ay1=(100 200 "abc")          ## 定义数组
[root@pc1 test]# echo ${ay1[*]}               ## 输出数组的所有元素
100 200 abc
[root@pc1 test]# unset ay1                    ## 删除数组
[root@pc1 test]# echo ${ay1[*]}               ## 确认删除效果

 

007、遍历数组

[root@pc1 test]# ay1=(100 "ab" "xy" 888 777)            ## 定义测试数组
[root@pc1 test]# for i in ${ay1[*]}; do echo $i; done   ## 遍历数组
100
ab
xy
888
777

 

008、数组切片

[root@pc1 test]# ay1=(100 "ab" "xy" 888 777 999 555 111 222 000)    ## 定义数组
[root@pc1 test]# echo ${ay1[*]}                                     ## 输出数组的元素
100 ab xy 888 777 999 555 111 222 000
[root@pc1 test]# echo ${ay1[*]:0:2}                                 ## 数组切片,前两个元素
100 ab
[root@pc1 test]# echo ${ay1[*]:2}                                   ## 从第三个元素,一直到最后
xy 888 777 999 555 111 222 000

 

009、关联数组的创建

普通数组 VS 关联数组

复制代码
[root@pc1 test]# dict1["a"]=100
[root@pc1 test]# dict1["b"]=200
[root@pc1 test]# dict1["c"]=300              ## 普通数组
[root@pc1 test]# echo ${dict1[*]}            ## 输出普通数组(序列数组)的所有元素
300
[root@pc1 test]# declare -A dict2            ## 声明关联数组
[root@pc1 test]# dict2["a"]=100
[root@pc1 test]# dict2["b"]=200
[root@pc1 test]# dict2["c"]=300
[root@pc1 test]# echo ${dict2[*]}            ## 关联数组和序列数组对比效果       
100 200 300
复制代码

 

010、关联数组单个元素的访问

[root@pc1 test]# declare -A dict2           ## 声明关联数组
[root@pc1 test]# dict2["a"]=100
[root@pc1 test]# dict2["b"]=200
[root@pc1 test]# dict2["c"]=300             ## 创建关联数组
[root@pc1 test]# echo ${dict2["b"]}         ## 访问关联数组单个元素
200

 

011、关联数组的遍历

复制代码
[root@pc1 test]# declare -A ay1                     ## 声明关联数组
[root@pc1 test]# ay1["a"]=100
[root@pc1 test]# ay1["b"]=200
[root@pc1 test]# ay1["c"]=300
[root@pc1 test]# echo ${!ay1[*]}                    ## 输出关联数组的下标
a b c
[root@pc1 test]# for i in ${!ay1[*]}; do echo "$i: ${ay1[$i]}"; done     ## 关联数组的遍历
a: 100
b: 200
c: 300
复制代码

 。

 

posted @   小鲨鱼2018  阅读(638)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-11-05 could not find function "read_excel"
2021-11-05 R语言中计算几何平均数
2021-11-05 R语言中计算变异系数
2021-11-05 R语言中统计频数分布
2020-11-05 什么是端口?
2020-11-05 linux系统中防火墙管理工具iptables
2020-11-05 linux系统中nmcli命令、查看、添加、删除、编辑网络会话
点击右上角即可分享
微信分享提示