shell脚本(6)-shell数组
2024-10-19 09:16:22
一、数组介绍
一个变量只能存一个值,现实中很多值需要存储,可以定义数组来存储一类的值。
二、基本数组
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
最新文章
- MYSQL 常用函数(数学、字符串、日期时间、系统信息、加密)
- 增加SharePoint2010修改域密码功能
- div居中的三种方法
- JS頁面值傳遞
- MySql数据库3【优化1】表的优化
- 创建.NET应用程序所经历的步骤
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- Python 自带IDLE中调试程序
- Swift - 网络请求报App Transport Security has blocked a cleartext错
- 线性表实现简单vector
- Python 用SMTP发送邮件
- webapi中Route标签定义可选参数
- 关于爬楼梯的lintcode代码
- tplink路由器DMZ设置
- 文件上传和WAF的攻与防
- 最简单的基于FFmpeg的AVfilter样例(水印叠加)
- ElementNotVisibleException: Message: element not visible
- 2018/03/19 每日一个Linux命令 之 touch
- linux 配置Tomcat开机启动
热门文章
- MySQL基础练习
- pytest初始化与清除(一)
- 【模板】Noi-Linux 下的一些配置
- 【题解】hdu2044一只小蜜蜂
- VLAN协议与三层交换机 (Access/Trubk/Hrbrid)
- Linux中重要目录详解
- GDI+中发生一般性错误的解决办法(转载)
- 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript.get”要求对象引用
- 【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
- 包机制与javaDOC文档