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

 。

 

posted @ 2023-12-12 10:37  小鲨鱼2018  阅读(266)  评论(0编辑  收藏  举报