linux 中shell数组的应用

 

001、生成数组, 生成数组a

root@ubuntu2204:/home/test# a[0]=800
root@ubuntu2204:/home/test# a[1]=500
root@ubuntu2204:/home/test# a[2]="kkk"
root@ubuntu2204:/home/test# a[3]="ggg"
root@ubuntu2204:/home/test# a[4]="xxx"
root@ubuntu2204:/home/test# a[5]=777
root@ubuntu2204:/home/test# a[6]=666

 

 

002、输出数组单个元素

root@ubuntu2204:/home/test# echo ${a[0]}
800
root@ubuntu2204:/home/test# echo ${a[4]}
xxx

 

 

003、批量输出数组元素

复制代码
root@ubuntu2204:/home/test# for i in $(seq 0 5); do echo ${a[i]}; done
800
500
kkk
ggg
xxx
777
root@ubuntu2204:/home/test# for i in $(seq 0 6); do echo ${a[i]}; done
800
500
kkk
ggg
xxx
777
666
复制代码

 

 

004、使用一行生成数组

root@ubuntu2204:/home/test# declare -a b=(111 "xxx" 555 800)
root@ubuntu2204:/home/test# echo ${b[1]}
xxx
root@ubuntu2204:/home/test# echo ${b[3]}
800

 

 

005、在1行中打印整个数组

root@ubuntu2204:/home/test# declare -a a=(100 200 "xxx" "yyy")
root@ubuntu2204:/home/test# echo ${a[@]}
100 200 xxx yyy

 

006、获取数组的长度

root@ubuntu2204:/home/test# declare -a a=(100 200 300 400)
root@ubuntu2204:/home/test# echo ${#a[@]}
4
root@ubuntu2204:/home/test# declare -a b=(100 200 300 400 "xxx" "yyy")
root@ubuntu2204:/home/test# echo ${#b[@]}
6

 

 

007、数组中单个元素的长度

复制代码
root@ubuntu2204:/home/test# declare -a a=(100 33333 2 44)
root@ubuntu2204:/home/test# echo ${#a[0]}
3
root@ubuntu2204:/home/test# echo ${#a[2]}
1
root@ubuntu2204:/home/test# echo ${#a[1]}
5
root@ubuntu2204:/home/test# echo ${#a[3]}
2
复制代码

 

008、数组切片

root@ubuntu2204:/home/test# a=(100 "xxx" 200 888 999 "yyy" "zzz")
root@ubuntu2204:/home/test# echo ${a[@]:2}   ## 索引为2一直到最后的元素
200 888 999 yyy zzz
root@ubuntu2204:/home/test# echo ${a[@]:2:3}   ## 索引为2后增加3个元素
200 888 999

 

 009、替换数组中的元素

[root@PC1 test2]# array=(a b c a d a k)
[root@PC1 test2]# echo ${array[@]}
a b c a d a k
[root@PC1 test2]# echo ${array[@]/a/QQ}   ## 将数组中的a替换为QQ
QQ b c QQ d QQ k

 

 

010、向数组中添加元素

001、

[root@PC1 test2]# echo ${array[@]}
a b c a d a k
[root@PC1 test2]# array=(${array[@]} II RR)
[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR

 

 

002、

[root@PC1 test2]# echo ${#array[@]}
9
[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR
[root@PC1 test2]# array[9]=100
[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR 100

 

 

011、从数组中删除一个元素

001、

[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR 100
[root@PC1 test2]# unset array[2]         ## 删除索引为2的数组元素
[root@PC1 test2]# echo ${array[@]}
a b a d a k II RR 100

 

 

002、

[root@PC1 test2]# array=(a b c a d a k)
[root@PC1 test2]# array=(${array[@]} II RR)
[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR
[root@PC1 test2]# array=(${array[@]:0:2} ${array[@]:(2+1)})
[root@PC1 test2]# echo ${array[@]}
a b a d a k II RR

 

 

003、利用正则删除数组中的元素

[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR
[root@PC1 test2]# echo ${array[@]/a*/}
b c d k II RR

 

 

012、复制数组

[root@PC1 test2]# echo ${array[@]}
a b c a d a k II RR
[root@PC1 test2]# array2=(${array[@]})
[root@PC1 test2]# echo ${array2[@]}
a b c a d a k II RR

 

 

013、数组的合并

[root@PC1 test2]# unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux')
[root@PC1 test2]# shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh')
[root@PC1 test2]# abc=("${unix[@]}" "${shell[@]}")
[root@PC1 test2]# echo ${abc[@]}
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh

 

 014、删除数组

[root@PC1 test2]# array=(a d r c k i)
[root@PC1 test2]# echo ${array[@]}
a d r c k i
[root@PC1 test2]# unset array
[root@PC1 test2]# echo ${array[@]}

 

 

015、将文件内容加载到数组中

复制代码
[root@PC1 test2]# ls
file.txt
[root@PC1 test2]# cat file.txt
aaa
bbb
ccc ddd
[root@PC1 test2]# array=($(cat file.txt))
[root@PC1 test2]# echo ${array[@]}
aaa bbb ccc ddd
复制代码

 

 

 

 

 

参考:https://www.linuxprobe.com/shell-array-example.html

 

posted @   小鲨鱼2018  阅读(347)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-06-26 redhat8重启网卡服务Failed to restart network.service: Unit network.service not found.
2020-06-26 linux系统安装bison,解决 These critical programs are missing or too old: bison compiler
2020-06-26 linux系统 binutils 的 安装,解决These critical programs are missing or too old: as GNU ld 的问题
2020-06-26 httpd-2.4.6-17.el7.x86_64: [Errno 256] No more mirrors to try 的解决方法
2020-06-26 linux 系统 如何 安装 python (python 3.8)
点击右上角即可分享
微信分享提示