linux 中 数组的常见操作
001、创建数组(三种方法)(下标连续数组和下标不连续数组)
a、利用小括号生成,各个元素之间使用空格间隔
[root@pc1 test01]# ay1=(111 222 aaa bbb) ## 生成普通数组 [root@pc1 test01]# echo ${ay1[*]} ## 访问数组的全部元素 111 222 aaa bbb
b、利用数组下标生成
[root@pc1 test01]# ay1[0]=111 [root@pc1 test01]# ay1[1]=222 [root@pc1 test01]# ay1[2]=333 [root@pc1 test01]# ay1[3]=aaa ## 利用数组下标直接生成 [root@pc1 test01]# echo ${ay1[*]} 111 222 333 aaa
c、利用小括号 + 字符串
[root@pc1 test01]# str1="111 abc xyz 777" ## 字符串 [root@pc1 test01]# ay1=($str1) ## 生成数组 [root@pc1 test01]# echo ${ay1[*]} 111 abc xyz 777
d、不连续下标
[root@pc1 test01]# ay1[0]=100 [root@pc1 test01]# ay1[3]=888 [root@pc1 test01]# echo ${ay1[*]} ## 下表不连续普通数组 100 888
002、访问数组(访问全部元素;访问单个元素)
[root@pc1 test01]# ay1=(111 aaa bbb 222) ## 测试数组 [root@pc1 test01]# echo ${ay1[*]} ## 访问数组的全部元素 111 aaa bbb 222 [root@pc1 test01]# echo ${ay1[0]} ## 访问数组的第一个元素 111 [root@pc1 test01]# echo ${ay1[3]} ## 访问数组的第四个元素 222 [root@pc1 test01]# echo ${ay1[5]} ## 越界访问, 为空
003、遍历数组(利用循环实现;for)
[root@pc1 test01]# str1="111 aaa bbb eee" ## 测试字符串 [root@pc1 test01]# ay1=($str1) ## 生成数组 [root@pc1 test01]# echo ${ay1[*]} 111 aaa bbb eee [root@pc1 test01]# for i in ${ay1[*]}; do echo $i; done ## 遍历数组 111 aaa bbb eee
004、输出数组的长度(下标连续和下标不连续)
a、连续下标
[root@pc1 test01]# ay1=(111 aaa bbb ccc ddd) ## 测试数组 [root@pc1 test01]# echo ${ay1[*]} 111 aaa bbb ccc ddd [root@pc1 test01]# echo ${#ay1[*]} ## 输出数组的长度 5
b、不连续下标
[root@pc1 test01]# ay1[0]=111 [root@pc1 test01]# ay1[3]=aaa [root@pc1 test01]# ay1[5]=kkk ## 生成不连续数组 [root@pc1 test01]# echo ${ay1[*]} 111 aaa kkk [root@pc1 test01]# echo ${#ay1[*]} ## 输出下表不连续数组的长度 3
005、输出数组的下标(下标连续和下标不连续)
a、连续下标
[root@pc1 test01]# ay1=(111 222 aaa bbb) [root@pc1 test01]# echo ${ay1[*]} ## 测试数组 111 222 aaa bbb [root@pc1 test01]# echo ${!ay1[*]} ## 输出数组的下标 0 1 2 3
b、不连续下标
[root@pc1 test01]# ay1[0]=111 [root@pc1 test01]# ay1[5]=aaa [root@pc1 test01]# ay1[8]=888 ## 生成不连续下标数组 [root@pc1 test01]# echo ${ay1[*]} 111 aaa 888 [root@pc1 test01]# echo ${!ay1[*]} ## 输出数组下标 0 5 8
006、输出数组中每个元素的长度
[root@pc1 test01]# ay1=(11 22222 kkk yyyyyy) ## 测试数组 [root@pc1 test01]# for ((i = 0; i < ${#ay1[*]}; i++)); do echo ${#ay1[i]}; done ## 输出数组中每个元素的长度 2 5 3 6
007、利用循环结构将元素保存在数组中(直接利用数组的下标添加/生成空格间隔的字符串,然后生成数组)
[root@pc1 test01]# ls a.txt [root@pc1 test01]# cat a.txt 001 002 003 004 005 [root@pc1 test01]# for i in $(cat a.txt); do str1="$str1 $i"; done ## 变量储存在字符串中 [root@pc1 test01]# echo $str1 001 002 003 004 005 [root@pc1 test01]# ay1=($str1) ## 利用字符串生成数组 [root@pc1 test01]# echo ${ay1[*]} 001 002 003 004 005
008、删除数组
a、删除数组中的某一个元素
[root@pc1 test01]# ay1=(aaa 111 222 333 kkk) ## 测试数组 [root@pc1 test01]# echo ${ay1[*]} aaa 111 222 333 kkk [root@pc1 test01]# unset ay1[2] ## 删除数组中的单个元素 [root@pc1 test01]# echo ${ay1[*]} aaa 111 333 kkk [root@pc1 test01]# unset ay1[0] ## 删除数组中的单个元素 [root@pc1 test01]# echo ${ay1[*]} 111 333 kkk
b、删除整个数组
[root@pc1 test01]# echo ${ay1[*]} 111 333 kkk [root@pc1 test01]# unset ay1 ## 删除整个数组 [root@pc1 test01]# echo ${ay1[*]}
009、向数组中追加元素
a、单个追加
[root@pc1 test01]# ay1=(aaa bbb 222 888) ## 测试数组 [root@pc1 test01]# echo ${ay1[*]} aaa bbb 222 888 [root@pc1 test01]# ay1[${#ay1[*]}]=99999 ## 单个元素追加 [root@pc1 test01]# echo ${ay1[*]} ## 追加效果 aaa bbb 222 888 99999
b、循环追加
01、
[root@pc1 test01]# ls a.txt [root@pc1 test01]# echo ${ay1[*]} [root@pc1 test01]# cat a.txt 01 02 03 [root@pc1 test01]# for i in $(cat a.txt); do ay1[${#ay1[*]}]=$i; done ## 向数组中追加元素 [root@pc1 test01]# echo ${ay1[*]} ## 输出数组 01 02 03
02、直接追加
[root@pc1 test01]# echo ${ay1[*]} [root@pc1 test01]# for i in {1..5}; do ay1+=($i); done ## 直接追加 [root@pc1 test01]# echo ${ay1[*]} 1 2 3 4 5
010、数组的合并
[root@pc1 test01]# ay1=(111 aaa) [root@pc1 test01]# ay2=(222 bbb) [root@pc1 test01]# ay3=(${ay1[*]} ${ay2[*]}) ## 数组的合并 [root@pc1 test01]# echo ${ay3[*]} ## 合并效果 111 aaa 222 bbb [root@pc1 test01]# echo ${!ay3[*]} 0 1 2 3
。
011、数组的切片
a、
[root@pc1 test01]# echo ${ay1[*]} ## 输出数组 11 22 33 44 55 66 77 88 99 [root@pc1 test01]# echo ${ay1[*]:0:2} ## 输出数组从0索引开始,向后输出两个元素 11 22 [root@pc1 test01]# echo ${ay1[*]:3:2} ## 从第三个元素开始 44 55
b、
[root@pc1 test01]# echo ${ay1[*]} 11 22 33 44 55 66 77 88 99 [root@pc1 test01]# echo ${#ay1[*]} 9 [root@pc1 test01]# echo ${ay1[*]:${#ay1[*]}-2} ## 取数组的最后两个元素 88 99 [root@pc1 test01]# echo ${ay1[*]:${#ay1[*]}-3} 77 88 99
。
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-12-12 R语言在最后一行追加字符
2021-12-12 linux shell在最后一行末尾追加字符
2021-12-12 windows下如何运行shell脚本
2020-12-12 linux中部署DNS分离解析技术
2020-12-12 一个IP可以对应多个域名,一个域名也可以对应多个ip?
2020-12-12 linux中BIND服务程序安全的加密传输TSIG机制
2020-12-12 linux系统中部署DNS从服务器