linux 中 数组的常见操作
001、创建数组
[root@PC1 test02]# ay=(1 2 3 4) ## 生成数组 [root@PC1 test02]# echo ${ay[*]} ## 输出数组 1 2 3 4 [root@PC1 test02]# echo ${#ay[*]} ## 输出数组的长度 4
002、
[root@PC1 test02]# ay=("a", "b", "c", "x") ## 字符串数组 [root@PC1 test02]# echo ${ay[*]} a, b, c, x [root@PC1 test02]# echo ${ay[@]} ## 输出数组 a, b, c, x [root@PC1 test02]# echo ${#ay[*]} ## 输出数组的长度 4
003、输出单个数组元素的值
[root@PC1 test02]# ay=("a", "b", "c", "x") ## 生成数组 [root@PC1 test02]# echo ${ay[0]} ## 输出单个元素的值 a, [root@PC1 test02]# echo ${ay[2]} c, [root@PC1 test02]# for i in {0..3}; do echo ${ay[$i]}; done ## 遍历 a, b, c, x
。
004、给数组的元素赋值
[root@PC1 test02]# ay=("x" "b" "k" "j") ## 生成数组 [root@PC1 test02]# echo ${ay[*]} ## 输出数组 x b k j [root@PC1 test02]# ay[4]="m" ## 给数组的单个元素赋值 [root@PC1 test02]# echo ${ay[*]} ## 输出数组 x b k j m
005、普通数组和关联数组
[root@PC1 test02]# declare array01 ## 定义普通数组 [root@PC1 test02]# declare -A array02 ## 定义关联数组 [root@PC1 test02]# array01["a"]=1000 [root@PC1 test02]# array01["b"]=2000 [root@PC1 test02]# array02["a"]=1000 [root@PC1 test02]# array02["b"]=2000 [root@PC1 test02]# echo ${array01[*]} ## 普通数组 2000 [root@PC1 test02]# echo ${array02[*]} ## 关联数组 1000 2000 [root@PC1 test02]# echo ${array01["a"]} 2000 [root@PC1 test02]# echo ${array02["a"]} ## 关联数组可以使用其他字符正确索引 1000
。
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-07-05 计算机没有声音,无法找到输出设备
2022-07-05 显示器指标
2021-07-05 linux系统中find命令
2021-07-05 linux系统中export命令
2021-07-05 linux系统中批量对一类文件重命名
2021-07-05 linux系统中修改别名配置文件,构建命令别名
2021-07-05 linux系统中tr命令