数组允许脚本利用索引将数据集合保存为独立的条目。Bash支持普通数组和关联数组,前者

使用整数作为数组索引,后者使用字符串作为数组索引。当数据以数字顺序组织的时候,应该使

用普通数组,例如一组连续的迭代。当数据以字符串组织的时候,关联数组就派上用场了,例如

主机名称。

普通数组

  1. 可以在单行中使用数值列表来定义一个数组
[root@dns-node2 tmp]# array_var=(test1 test2 test3)
[root@dns-node2 tmp]# echo ${array_var[0]}
test1

另外,还可以将数组定义成一组“索引-值”

[root@dns-node2 tmp]# array_var[0]="test0"
[root@dns-node2 tmp]# array_var[0]="test3"
[root@dns-node2 tmp]# array_var[2]="test2"
[root@dns-node2 tmp]# array_var[3]="test0"
[root@dns-node2 tmp]# echo ${array_var}
test3
[root@dns-node2 tmp]# echo ${array_var[3]}
test0
[root@dns-node2 tmp]# echo ${array_var[2]}
test2
[root@dns-node2 tmp]# echo ${array_var[1]}
test2
[root@dns-node2 tmp]# echo ${array_var[0]}
test3

打印这个数组下面所有的值

[root@dns-node2 tmp]# echo ${array_var[*]}
test3 test2 test2 test0
[root@dns-node2 tmp]# echo ${array_var[@]}
test3 test2 test2 test0

打印数组的长度

[root@dns-node2 tmp]# echo ${#array_var[@]}
4

关联数组

在关联数组中,我们可以用任意的文本作为数组索引。首先,需要使用声明语句将一个变量,这个其实是Map,在python里面叫做字典,在go里面叫做map。

定义为关联数组

使用行内“索引-值”列表:
[root@dns-node2 tmp]# declare -A ass_array
[root@dns-node2 tmp]# ass_array=([i1]=v1 [i2]=v2)
使用独立的“索引-值”进行赋值
[root@dns-node2 tmp]# ass_array=[i1]=v1
[root@dns-node2 tmp]# ass_array=[i2]=v2

取值

[root@dns-node2 tmp]# echo ${ass_array[i1]}
v1
[root@dns-node2 tmp]# echo ${ass_array[i2]}
v2
[root@dns-node2 tmp]# echo ${ass_array[*]} # 取value
v2 v1
[root@dns-node2 tmp]# echo ${ass_array[@]} # 取value
v2 v1
[root@dns-node2 tmp]# echo ${!ass_array[@]} #取key
i2 i1

shell基础知识之数组的更多相关文章

  1. Linux shell基础知识(上)

    Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...

  2. Linux Shell 基础知识(一)

    1. 本文知识结构 2. shell 基础知识 2.1 shell 简单介绍 ​ GNU bash shell 能提供对 Linux 系统的交互式访问,一般来说,使用快捷键 Ctrl + Alt + ...

  3. shell基础知识总结

    1. shell 对于一台计算机而言,其硬件受系统内核的控制,使用者想要控制计算机,就必须有与系统内核进行通讯的手段.而shell就是使用者与计算机进行通讯的手段之一.从命名上看,shell其实是相对 ...

  4. shell基础知识---与监听服务器长连接端口状态

    从未写过脚本我的最近接了俩脚本的需求,就在这分享一下我的我学到基础知识主要就四部分内容 一.变量 变量的定义 string='字符串' string="字符串" num=808st ...

  5. Shell 基础知识和总结

    调试脚本 检查脚本语法错误 bash -n /path/to/some_script 调试执行 bash -x /path/to/some_script shell里的变量 本地变量:只对当前shel ...

  6. shell基础知识讲解

    第1章 shell基础 1.1 什么叫做shell编程 shell编程也叫做bash高级编程语法 1.2 常见的shell命令解释器 bash            redhat和centos使用 d ...

  7. Shell基础知识(五)

    shell中同样有数组的概念,获取数组中的元素要使用下标[],并且下标的值必须大于等于0.数据的各项特性见下例: #!/bin/bash array1=(1 2 3 999) echo ${array ...

  8. shell基础知识

    Shell 学习基础 1.组合命令的符号 管道,将前面一个命令的结果作为后面一个命令的输入 分号,顺序执行用分号分割的命令 重定向,重定向包括三种:输入重定向.输出重定向.错误重定向,以7个不同的符号 ...

  9. shell从入门到精通进阶之一:Shell基础知识

    1.1 简介 Shell是一个C语言编写的脚本语言,它是用户与Linux的桥梁,用户输入命令交给Shell处理,Shell将相应的操作传递给内核(Kernel),内核把处理的结果输出给用户. 下面是处 ...

随机推荐

  1. 使用MPU6050陀螺仪自制Arduino数字量角器

    MPU6050惯性单元是一个3轴加速度计和一个3轴陀螺仪组合的单元.它还包含温度传感器和DCM,可执行复杂的任务. MPU6050通常用于制作无人机和其他远程控制机器人,如自平衡机器人.在本篇文章中, ...

  2. Python决策树可视化:GraphViz's executables not found的解决方法

    参考文献: [1]Python决策树可视化:GraphViz's executables not found的解决方法

  3. matplotlib---插值画二维、三维图

    一.画二维图 1.原始数据(x,y) import matplotlib.pyplot as plt import numpy as np #数据 X = np.array(list(i for i ...

  4. Android AMS服务

    继续来研究Android Framework层相关的一些东东,这里是以Android8.0版本的源码进行梳理的,关注的还是其核心流程,不是彻底分析,了解了核心流程是为了了期其大概的原理. Androi ...

  5. JAVA之Socket通讯

    Server.java: Client.java Server console:(先启动服务器,再启动客户端)  服务器读取了客户端发来的hello server: Client console:客户 ...

  6. springboot ResponseEntity<byte[]> 下载文件 byte 都变成base64

    因为spring boot消息转换器 ,全部将数据转换为json格式,包括文件的byte数据 关于spring boot 的消息转换器见:https://www.jianshu.com/p/ffe56 ...

  7. font-awesome图标显示问题解决方案

    font-awesome一个很强大的字体图标库.下载链接:http://fontawesome.dashgame.com/刚开始使用font-awesome的新手往往容易只引入一个css文件,这样就会 ...

  8. 项目Beta冲刺--6/7

    项目Beta冲刺--6/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...

  9. table的各种用法

    使用 colgroup 和 col 实现响应式表格(table的各种用法):http://coderlt.coding.me/2017/11/20/table-colgroup/

  10. JUnit 学习资料

    JUnit 学习资料 网址 JUnit 入门教程(极客学院) http://wiki.jikexueyuan.com/project/junit/ 官方网站 https://junit.org/jun ...