shell脚本(6)-shell数组
一、数组介绍
一个变量只能存一个值,现实中很多值需要存储,可以定义数组来存储一类的值。
二、基本数组
1、概念:
数组可以让用户一次性赋予多个值,需要读取数据时只需通过索引调用就可以方便读出。
2、数组语法
数组名称=(元素1 元素2 元素3)
[root@localhost test20210725]# list1=(1 2 3 4 5)
[root@localhost test20210725]# list2=('a' 'b' 'c' 'd')
3、数组读出
${数组名称[索引]}
索引默认是元素在数组中的排队编号,默认第一个从0开始
[root@localhost test20210725]# list1=(1 2 3 4 5)
[root@localhost test20210725]# list2=('a' 'b' 'c' 'd')
[root@localhost test20210725]# echo ${list1[0]}
1
[root@localhost test20210725]# echo ${list2[2]}
c
4、数组赋值
[root@localhost test20210725]# list1=(1 2 3 4 5)
[root@localhost test20210725]# list1[0]='1a'
[root@localhost test20210725]# echo ${list1[0]}
1a
5、查看声明过的数组
[root@localhost test20210725]# declare -a
declare -a BASH_ARGC='()'
declare -a BASH_ARGV='()'
declare -a BASH_LINENO='()'
declare -a BASH_SOURCE='()'
declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")'
declare -a DIRSTACK='()'
declare -a FUNCNAME='()'
declare -a GROUPS='()'
declare -a PIPESTATUS='([0]="127")'
declare -a list1='([0]="1a" [1]="2" [2]="3" [3]="4" [4]="5")'
declare -a list2='([0]="a" [1]="b" [2]="c" [3]="d")'
6、访问数组元素
[root@localhost test20210725]# list1=(1 2 3 4 5 6 7 8 9 0)
[root@localhost test20210725]# echo ${list1[0]} #访问数组中第一个元素
1
[root@localhost test20210725]# echo ${list1[@]} #访问数组中所有元素,@等同于*
1 2 3 4 5 6 7 8 9 0
[root@localhost test20210725]# echo ${list1[*]}
1 2 3 4 5 6 7 8 9 0
[root@localhost test20210725]# echo ${#list1[@]} #统计数组中元素个数
10
[root@localhost test20210725]# echo ${!list1[@]} #统计数组元素的索引
0 1 2 3 4 5 6 7 8 9
[root@localhost test20210725]# echo ${list1[@]:1} #从数组下标1开始
2 3 4 5 6 7 8 9 0
[root@localhost test20210725]# echo ${list1[@]:1:3} #从数组下标1开始,访问3个元素
2 3 4
7、遍历数组
(1)默认数组通过数组元素的个数进行遍历
vim list_for.sh
#!/bin/bash list="rootfs usr data data2" for i in $list; do echo $i is appoint ; done
查看运行结果:
[root@localhost test20210725]# sh list_for.sh
rootfs is appoint
usr is appoint
data is appoint
data2 is appoint
三、关联数组
1、概念:
关联数组可以允许用户自定义数组的索引,这样使用起来更加方便、高效
2、定义关联数组:
[root@localhost test20210725]# declare -A acc_array1 #声明一个关联数组
3、关联数组赋值:
[root@localhost test20210725]# declare -A acc_array1 #声明一个关联数组
[root@localhost test20210725]# acc_array1=([name]='mrwhite' [age]=18) #赋值
4、关联数组查询:
[root@localhost test20210725]# declare -A acc_array1 #声明一个关联数组
[root@localhost test20210725]# acc_array1=([name]='mrwhite' [age]=18) #赋值
[root@localhost test20210725]# echo ${acc_array1[name]}
mrwhite
5、关联数组的遍历:
[root@localhost test20210725]# vim ass_list_for.sh
#!/usr/bin/bash
#################################
# Author: Mr.white #
# Create_Date: 2021-07-03 19:09:56 #
# Version: 1.0 #
#################################
declare -A acc_list
acc_list=([name]='mrwhite' [age]=18)
echo "数组acc_list的key value为:"
for key in ${!acc_list[@]}
do
#根据key取值
echo "$key <-> ${acc_list[${key}]}"
done
查询运行结果:
[root@localhost test20210725]# sh ass_list_for.sh
数组acc_list的key value为:
name <-> mrwhite
age <-> 18
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了