shell脚本(6)-shell数组

摘要:
2、数组语法数组名称=[root@localhosttest20210725]#list1=[root@localhosttest20210725]#list2=3、数组读出${数组名称[索引]}索引默认是元素在数组中的排队编号,默认第一个从0开始[root@localhosttest20210725]#list1=[root@localhosttest20210725]#list2=[root@localhosttest20210725]#echo${list1[0]}1[root@localhosttest20210725]#echo${list2[2]}c4、数组赋值[root@localhosttest20210725]#list1=[root@localhosttest20210725]#list1[0]='1a'[root@localhosttest20210725]#echo${list1[0]}1a5、查看声明过的数组[root@localhosttest20210725]#declare-adeclare-aBASH_ARGC='()'declare-aBASH_ARGV='()'declare-aBASH_LINENO='()'declare-aBASH_SOURCE='()'declare-arBASH_VERSINFO=''declare-aDIRSTACK='()'declare-aFUNCNAME='()'declare-aGROUPS='()'declare-aPIPESTATUS=''declare-alist1=''declare-alist2=''6、访问数组元素[root@localhosttest20210725]#list1=[root@localhosttest20210725]#echo${list1[0]}#访问数组中第一个元素1[root@localhosttest20210725]#echo${list1[@]}#访问数组中所有元素,@等同于*1234567890[root@localhosttest20210725]#echo${list1[*]}1234567890[root@localhosttest20210725]#echo${#list1[@]}#统计数组中元素个数10[root@localhosttest20210725]#echo${!

一、数组介绍

一个变量只能存一个值,现实中很多值需要存储,可以定义数组来存储一类的值。

二、基本数组

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; doecho $i isappoint ; done

查看运行结果:

[root@localhost test20210725]# sh list_for.sh 
rootfs isappoint
usr isappoint
data isappoint
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

免责声明:文章转载自《shell脚本(6)-shell数组》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇博弈论知识汇总OpenTelemetry架构介绍下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux(Ubuntu)设置环境变量(转载)

http://blog.csdn.net/wumingxing0228/article/details/6050175 环境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell。对于Linux来说一般是bash,但也可以重新设定或切换到其它的 Shell。对于UNIX,可能是CShelll。环境变量是通过Shell命令来设置的,设置好的环境变...

shell 正则表达式与文件名匹配

1) . : 匹配任意单ASCII 字符,可以为字母,或为数字。 2) 举例: ..XC..匹配deXC1t、23XCdf 等,.w..w..w.匹配rwxrw-rw-行首以^匹配字符串或字符序列 1) ^ : 允许在一行的开始匹配字符或单词。 2) 举例: ^.01 匹配0011cx4、c01sdf 等,^d 匹配drwxr-xr-x、drw-r--r-...

02_编程规约——集合处理

1.【强制】关于hashCode和equals的处理,必须遵循如下规则 1.1 只要重写equals,就必须重写hashCode。 1.2 因为Set存储的是不重复对象,依据hashCode和equals进行判断,所以Set存储的对象必须重写这两个方法。 1.3 如果自定义对象为Map的键,那么必须重写hashCode和equals。 说明:String重...

利用shell脚本调用ansible自动化实现企业备份基本环境

inotify+rsync实时监控推送NFS挂载目录脚本 #!/bin/bash Path=/data /usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | whileread line do if [ -f $line ];the...

vue+elementui +input输入框关键字筛选检索表格数据展示+分页功能

第一种用axios发送请求到后台,需要后台配合,才能在表格里面渲染页面;想偷懒的小伙建议去直接粘贴复制第三种 <template> <div class="tableDatas"> <div class="searchWord"> <div style="display: inline-block"> 搜索...

C/C++ 关于数组和指针的总结

1、数组的声明形如a[d],其中a是数组的名字,d是数组的维度,编译的时候数组的维度应该是已知的,所以维度d必须是一个常量。如果要定义一个不知道元素个数的以为数组,那么请使用vector容器; unsigned cnt = 42; //不是常量表达式 constexpr unsigned sz = 42; //常量表达式 int arr...