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
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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)