linux shell编程中的数组

 

001、普通数组

普通数组的定义: 

ay=(100 200 300 "aa" "bb")             ## 直接定义

 

[root@PC1 test]# ay2[0]=800           ## 单独定义每一个元素
[root@PC1 test]# ay2[1]="aaaa"
[root@PC1 test]# ay2[2]=5.834

 

[root@PC1 test]# cat a.txt
a b
c d
a b
e f
c d
c d
m n
[root@PC1 test]# ay3=$(cat a.txt )          ## 通过命令定义数组
[root@PC1 test]# for i in ${ay3[@]}; do echo $i; done
a
b
c
d
a
b
e
f
c
d
c
d
m
n

 

002、数组的取值

[root@PC1 test]# ay=("aaa" 100 555 "kkk")
[root@PC1 test]# echo ${ay[0]}            ## 单个数组的取值,通过索引值的编号进行
aaa
[root@PC1 test]# echo ${ay[3]}
kkk

 

[root@PC1 test]# ay=("aaa" 100 555 "kkk")
[root@PC1 test]# for i in ${ay[@]}; do echo $i; done           ## 数组的遍历, 连续取值
aaa
100
555
kkk

 

[root@PC1 test]# echo ${ay[@]}                    ## 输出数组所有的值
aaa 100 555 kkk

 

 003、输出数组的长度

[root@PC1 test]# ay=("aaa" 100 555 "kkk")
[root@PC1 test]# echo ${#ay[@]}              ## 输出数组的长度
4

 

002、关联数组

 

posted @ 2023-02-24 23:48  小鲨鱼2018  阅读(50)  评论(0编辑  收藏  举报