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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM