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

          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.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 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[0]=CEO                #逐一对数组进行赋值
[root@node101.yinzhengjie.org.cn ~]# title[1]=COO
[root@node101.yinzhengjie.org.cn ~]# title[2]=CTO
[root@node101.yinzhengjie.org.cn ~]# title[3]=CFO
[root@node101.yinzhengjie.org.cn ~]# title[4]=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[1]}             #查看指定的元素下标
COO
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo ${title[3]}
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]                     #删除索引为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[@]}
5
[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啦~
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[@]}
5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# unset title
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]}
0
[root@node101.yinzhengjie.org.cn ~]#

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

[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]}
0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# title=([0]=CEO [3]=CTO [5]=devops)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo ${#title[@]}
3
[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[*]}
4
[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 ~]# 

5>.显示所有数组

[root@node101.yinzhengjie.org.cn ~]# student[0]="jason"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# student[1]="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:             1053419035
#Date:             2019-11-29
#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[0]=CEO
title[1]=COO
title[2]=CTO
title[3]=CFO
title[4]=devops

count=`seq 0 $[${#title[*]}-1]`

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[*]:1:2}             #跳过第一个元素并取出后续2个元素
COO CTO
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo ${title[*]:2}               #跳过前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"]=18
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo ${student_info["name"]}      #我们发现查询结果和咱们预期的不符哟!
18
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo ${student_info["age"]}
18
[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"]=18
[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"]}
18
[root@node101.yinzhengjie.org.cn ~]# 

 

五.小试牛刀

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

[root@node101.yinzhengjie.org.cn ~]# cat shell/max_min.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-29
#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=0;i<10;i++));do
    nums[$i]=$RANDOM
    [ $i -eq 0 ] && 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 12294 23970 23546 2585 30109 8170 18085 14324 9064 16452
Max is 30109
Min is 2585
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are 26022 2486 3472 95 28280 14681 6597 23915 18902 5125
Max is 28280
Min is 95
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are 31524 6762 19863 31838 5255 13273 9731 7561 12069 25468
Max is 31838
Min is 5255
[root@node101.yinzhengjie.org.cn ~]# 
参考案例1
[root@node101.yinzhengjie.org.cn ~]# cat shell/max_min.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-29
#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=0;i<10;i++));do
    nums[$i]=$RANDOM
    if [ $i -eq 0 ];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 13450 20504 23399 23270 15422 5545 17712 2157 24887 77
Max is 24887
Min is 77
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are 18948 20733 6480 1389 22489 30295 21598 32705 28384 26499
Max is 32705
Min is 1389
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/max_min.sh
All numbers are 27717 18726 30016 25075 17564 28546 28358 12498 14846 29631
Max is 30016
Min is 12498
[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:             1053419035
#Date:             2019-11-29
#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

 

 

posted @ 2019-11-21 22:24  尹正杰  阅读(2163)  评论(0编辑  收藏  举报