搜索
您的当前位置:首页正文

shell脚本(6)-shell数组

来源:意榕旅游网
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 -adeclare -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 appointusr is appointdata is appointdata2 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 <-> mrwhiteage <-> 18

因篇幅问题不能全部显示,请点此查看更多更全内容

Top