SHELL脚本编程-普通数组(列表)和关联数组(字典)

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.数组相关概述

变量:
  存储单个元素的内存空间 数组:
  存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引
  索引:编号从0开始,属于数值索引
  注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash4.0版本之后开始支持(关联数据使用时必须提前声明类型)
  bash的数组支持稀疏格式(索引不连续) 声明数组:
  declare -a ARRAY_NAME
  declare -A ARRAY_NAME: 关联数组
  注意:普通数组不用事先声明就可以使用,但关联数组必须提前声明才能使用,且两者不可相互转换。
[root@node101.yinzhengjie.org.cn ~]# bash --version
GNU bash, version 4.2.()-release (x86_64-redhat-linux-gnu)
Copyright (C) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bash --version

二.数组赋值

1>.一次只赋值一个元素及引用数组详解

[root@node101.yinzhengjie.org.cn ~]# declare -a title            #使用数组变量时建议提前声明
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# title[]=CEO   #逐一对数组进行赋值
[root@node101.yinzhengjie.org.cn ~]# title[]=COO
[root@node101.yinzhengjie.org.cn ~]# title[]=CTO
[root@node101.yinzhengjie.org.cn ~]# title[]=CFO
[root@node101.yinzhengjie.org.cn ~]# title[]=devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $title   #如果不加下标则默认打印数组的第一个元素。
CEO
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[]}      #查看指定的元素下标
COO
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[]}
CFO
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}          #查看数组中的所有元素
CEO COO CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}          #同上
CEO COO CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]}          #查看数组的长度
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[*]}          #同上
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}
CEO COO CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# title[${#title[*]}]="bigdata" #在数组的末尾添加一个元素
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}
CEO COO CTO CFO devops bigdata
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# unset title[] #删除索引为1的元素
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}
CEO CTO CFO devops bigdata
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# unset title #删除整个数组
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]} #由于数组依旧被删除了,长度自然也就为0啦~ [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}             #内容也被随之清空了 [root@node101.yinzhengjie.org.cn ~]#

2>.一次赋值全部元素

[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# title=(CEO COO CTO CFO devops)        #一次性赋值多个元素
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}
CEO COO CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# unset title
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]} [root@node101.yinzhengjie.org.cn ~]#

3>.只赋值特定元素(稀疏格式)

[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]}

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# title=([]=CEO []=CTO []=devops)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}
CEO CTO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# unset title
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]} [root@node101.yinzhengjie.org.cn ~]#

4>.交互式数组值对赋值

[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# read -a title        #通过交互式对数组title进行赋值,各个元素用空格分开即可。
CTO CEO COO CTO
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}
CTO CEO COO CTO
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[*]} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# unset title
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[*]} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

5>.显示所有数组

[root@node101.yinzhengjie.org.cn ~]# student[]="jason"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# student[]="yinzhengjie"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# declare -a      #显示当前shell中识别的所有数组
declare -a BASH_ARGC='()'
declare -a BASH_ARGV='()'
declare -a BASH_LINENO='()'
declare -ar BASH_REMATCH='()'
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]="0")'
declare -a student='([0]="jason" [1]="yinzhengjie")'
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

三.遍历数组

[root@node101.yinzhengjie.org.cn ~]# cat shell/title_array.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/title_array.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** declare -a title
title[]=CEO
title[]=COO
title[]=CTO
title[]=CFO
title[]=devops count=`seq $[${#title[*]}-]` for i in $count
do
echo Title is ${title[$i]}
done
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/title_array.sh
Title is CEO
Title is COO
Title is CTO
Title is CFO
Title is devops
[root@node101.yinzhengjie.org.cn ~]#

 

四.数组数据处理

1>.引用数组中的元素(数组切片)

[root@node101.yinzhengjie.org.cn ~]# title=(CEO COO CTO CFO devops)      #定义数组
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}               #查看数组的内容
CEO COO CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]::}             #跳过第一个元素并取出后续2个元素
COO CTO
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]:}               #跳过前2个元素,后面的元素全都要
CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.向数组中追加元素

[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]}
CEO COO CTO CFO devops
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# title[${#title[*]}]="bigdata" #在数组的末尾添加一个元素
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${title[@]}
CEO COO CTO CFO devops bigdata
[root@node101.yinzhengjie.org.cn ~]#

3>.关联数组(在其它编程语言称之为"字典"或者"map")

[root@node101.yinzhengjie.org.cn ~]# declare -a student_info        #声明为普通数组
[root@node101.yinzhengjie.org.cn ~]# student_info["name"]="尹正杰"
[root@node101.yinzhengjie.org.cn ~]# student_info["age"]=
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${student_info["name"]}      #我们发现查询结果和咱们预期的不符哟! [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${student_info["age"]} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# unset student_info           #将上面的普通数组删除掉,然后使用关联数组来保存数据。
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# declare -A student_info        #声明为关联数组
[root@node101.yinzhengjie.org.cn ~]# student_info["name"]="尹正杰"
[root@node101.yinzhengjie.org.cn ~]# student_info["age"]=
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${student_info["name"]}     #发现关联数组的确可以保存咱们的变量呢
尹正杰
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ${student_info["age"]} [root@node101.yinzhengjie.org.cn ~]#

五.小试牛刀

1>.生成10个随机数保存于数组中,并找出其最大值和最小值

[root@node101.yinzhengjie.org.cn ~]# cat shell/max_min.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/max_min.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** declare -i min max
declare -a nums for ((i=;i<;i++));do
nums[$i]=$RANDOM
[ $i -eq ] && min=${nums[$i]} && max=${nums[$i]}&& continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]}
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done echo "All numbers are ${nums[*]}"
echo "Max is $max"
echo "Min is $min"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are
Max is
Min is
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are
Max is
Min is
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are
Max is
Min is
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/max_min.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/max_min.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** declare -i min max
declare -a nums for ((i=;i<;i++));do
nums[$i]=$RANDOM
if [ $i -eq ];then
max=${nums[$i]}
min=${nums[$i]}
else
if [ "$max" -lt "${nums[$i]}" ];then
max=${nums[$i]}
elif [ "$min" -gt "${nums[$i]}" ];then
min=${nums[$i]}
else
true
fi
fi
done echo "All numbers are ${nums[*]}"
echo "Max is $max"
echo "Min is $min"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are
Max is
Min is
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are
Max is
Min is
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are
Max is
Min is
[root@node101.yinzhengjie.org.cn ~]#

参考案例2

2>.编写脚本,定义一个数组,数组中的元素对应的值是/var/log目录下所有以.log结尾的文件;统计出其下标为奇数的文件中的行数之和

[root@node101.yinzhengjie.org.cn ~]# cat shell/log_wc.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/log_wc.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** declare -a files
files=(/var/log/*.log)
declare -i lines=0 for i in $(seq 0 $[${#files[*]}-1]); do
if [ $[$i%2] -eq 1 ];then
let lines+=$(wc -l ${files[$i]} | cut -d' ' -f1)
fi
done echo "Lines: $lines."
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /var/log/*.log
-rw-------. 2 root root 0 Nov 28 07:41 /var/log/boot.log
-rw-------. 1 root root 15539 Nov 23 07:04 /var/log/yum.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# wc -l /var/log/boot.log
0 /var/log/boot.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# wc -l /var/log/yum.log
256 /var/log/yum.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/log_wc.sh
Lines: 256.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例

3>.输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

4>.打印杨辉三角形

5>.将下图所示,实现转置矩阵matrix.sh

SHELL脚本编程-普通数组(列表)和关联数组(字典)的更多相关文章

  1. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  2. Linux Shell脚本编程-数组和字符串处理

    数组  1.数组的定义及声明 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名:整个数组只有一个名字 索引:编号从0开始,属于数值索引:bash的数组支持 ...

  3. SHELL脚本编程的常识和VI常用技巧

    来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...

  4. 浅谈自底向上的Shell脚本编程及效率优化

    作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...

  5. Shell脚本编程30分钟入门

    Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...

  6. Shell脚本编程具体解释

    第12章 Shell脚本编程   l  Shell命令行的执行 l  编写.改动权限和运行Shell程序的步骤 l  在Shell程序中使用參数和变量 l  表达式比較.循环结构语句和条件结构语句 l ...

  7. 30分钟快速学习Shell脚本编程

    什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...

  8. 《Linux命令行与shell脚本编程大全》第二十二章 gawk进阶

    gawk是一门功能丰富的编程语言,你可以通过它所提供的各种特性来编写好几程序处理数据. 22.1 使用变量 gawk编程语言支持两种不同类型的变量: 内建变量和自定义变量 22.1.1 内建变量 ga ...

  9. 《Linux命令行与shell脚本编程大全 第3版》

    第一部分 Linux 命令行 第1章  初识Linux she1.1   什么是Linux 21.1.1 深入探究Linux 内核 31.1.2 GNU 工具 61.1.3 Linux 桌面环境 81 ...

随机推荐

  1. SVM – 核函数

    核函数的起源是对于线性不可分的分类情况,其实可以通过p次方多项式,及非线性模型进行分类:然后对于这类非线性多次方的,其实可以按照广义线性模型来进行升维变形,使之成为线性模型,这样就可以放到SVM中来进 ...

  2. aliyun手记

    阿里云里面购买的带宽是指外网带宽,内网默认是千兆带宽,做过I/O优化的则是万兆带宽. 修改密码实在更多(三个点)的那里进行修改的:修改密码(windows是administrator以及Linux是r ...

  3. [Golang] Gin框架学习笔记

    0x0 Gin简介 1.Gin 是什么? Gin 是一个用 Go (Golang) 编写的 HTTP web 框架. 它是一个类似于 martini 但拥有更好性能的 API 框架, 由于 httpr ...

  4. OCR(Optical Character Recognition)算法总结

    https://zhuanlan.zhihu.com/p/84815144 最全OCR资料汇总,awesome-OCR

  5. mysql 安装为服务 ,mysql.zip 安装为服务,mysql搬移迁移服务器安装为服务

    从服务器A打包到服务器B后,在服务器B中运行安装服务命令,可自定义服务名,一台服务器上可装N个MySql实例 mysqld --install MySQL_0001 --defaults-file=D ...

  6. jmeter jtl 文件

    一.获取.jtl文件 使用非 GUI 模式,即命令行模式运行 JMeter .执行完成jmeter后,会生成jtl文件. 1.1. 命令介绍 1)先cmd进入到jmeter的bin文件目录下(这里是 ...

  7. 在JDBC中实现SQL语句的模糊查询

    在JDBC中实现SQL语句的模糊查询 在大多数情况下我们可以在JDBC中写入sql语句通过占位符的方式来直接查询,但是如果要进行模糊查询,需要转义字符才能够正常查询. sql语句: select * ...

  8. 《学渣Linux笔记》——关于.bashrc与profile(涉及交互式与非交互式、登录与非登录shell)

    <学渣Linux笔记>--关于.bashrc与profile(涉及交互式与非交互式.登录与非登录shell) 1.基本概念(个人理解) 交互式shell:等待用户输入,并执行相应操作的sh ...

  9. OracleVM桥接网卡无法获取本地连接网卡

    问题现象 VM虚拟机采用桥接网卡时,界面名称为"未指定",无法获取本地连接对应网卡信息: 处理方式: 进入本地连接,选择本地连接右键进入属性设置窗口; 选择安装,单击服务选项后点击 ...

  10. ZooKeeper学习笔记(一)——概述

    zookeeper学习笔记(一)--概述 1. 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目.zookeeper从设计模式的角度来理解:是一个基于观察者设计 ...