shell

shell.2.9 bash脚本编程:数组

 

 

目录:
=================

1、数组的表达方式
1.1  数组中元素的赋值方式
1.2  关联数组的表示方式

2、 数组的应用
2.1  数组的长度表示方式(数组的元素个数、数组的所有元素、数组中第一个元素的字符个数)
2.2  数组元素切片
2.3  向数组中追加元素
2.4  删除数组中的某个元素

导语:
=================

变量:存储单个元素的内存空间;
数组:存储多个元素的连续的内存空间;

数组名:整个数组只有一个名字;
数组索引:编号从数字0开始;

数组格式:数组名[索引]
bash版本4以后支持自定义索引格式,索引可使用数字以外的字符,此类数组称为”关联数组”;
对数组进行变量引用的格式:${ARRAY_NAME[INDEX]}

声明数组的方式:
declare  -a  NAME                    声明索引数组;
declare  -A  NAME                   声明关联数组;

正文:
======================

1、数组的表达方式
1.1 数组中元素的赋值方式
1.2 关联数组的表示方式

1.1 数组中元素的赋值方式:
>>  bash支持稀疏格式的数组:就是说可以指定第一个元素’0’,然后跳到第5个元素’5’,没有指定的元素,其值为空;
>>  引用数组时如果不带数组索引,则默认显示第’0’号索引的元素;
>>  下面方式中,”ARRAY_NAME”表示数组名,”INDEX”表示数组索引的数字序号,”VALUE”表示具体的元素;

1> 一次只赋值一个元素;ARRAY_NAME[INDEX]=VALUE

[root@KOU ~]# 
[root@KOU ~]# animals[1]=pig
[root@KOU ~]# 
[root@KOU ~]# animals[3]=dog
[root@KOU ~]# 
[root@KOU ~]# echo ${animals[0]}
pig
[root@KOU ~]# 
[root@KOU ~]# echo ${animals[3]}
dog
[root@KOU ~]# 
[root@KOU ~]# echo ${animals[1]}
pig
[root@KOU ~]# echo ${animals[2]}

[root@KOU ~]# 

[root@KOU ~]# animals[1]=cat
[root@KOU ~]# 
[root@KOU ~]# echo ${animals[0]}
pig
[root@KOU ~]# echo ${animals[1]}
cat
[root@KOU ~]#

2> 一次赋值全部元素;ARRAY_NAME=(“VALUE1”  “VALUE2”  “VALUE3”  …)

[root@KOU ~]# 
[root@KOU ~]# weekdays=("monday" "sunday" "wednesday")
[root@KOU ~]# 
[root@KOU ~]# echo ${weekdays[0]}
monday
[root@KOU ~]# echo ${weekdays[1]}
sunday
[root@KOU ~]# echo ${weekdays[2]}
wednesday
[root@KOU ~]# echo ${weekdays[3]}

[root@KOU ~]#

3> 只赋值特定元素;ARRAY_NAME=([0]=”VALUE1″  [4]=”VALUE3″  …)

[root@KOU ~]# 
[root@KOU ~]# jinrong=([0]="shediao" [3]="yitiantulong" [8]="shengdiao")
[root@KOU ~]# 
[root@KOU ~]# echo ${jinrong[0]}
shediao
[root@KOU ~]# 
[root@KOU ~]# echo ${jinrong[2]}

[root@KOU ~]# echo ${jinrong[3]}
yitiantulong
[root@KOU ~]#

4> read  -a  ARRAY_NAME;

[root@KOU ~]# 
[root@KOU ~]# read -a huashan
dongxue xidu nandi beigai wangchongyang
[root@KOU ~]# 
[root@KOU ~]# echo ${huashan[0]}
dongxue
[root@KOU ~]# echo ${huashan[1]}
xidu
[root@KOU ~]#

 

1.2 关联数组的表示方式,先要定义关联数组”declare -A NAME”,然后才对元素赋值;

declare  -A  ARRAY_NAME
ARRAY_NAME[asso_name1]=VALUE1
ARRAY_NAME=([asso_name1]=”VALUE1″   [asso_name2]=”VALUE2″  …)

[root@KOU ~]# 
[root@KOU ~]# declare -A riben
[root@KOU ~]# 
[root@KOU ~]# riben[rb1]=tokyo
[root@KOU ~]# 
[root@KOU ~]# riben[rb3]=oosaka
[root@KOU ~]# 
[root@KOU ~]# riben[rb4]=kyotou
[root@KOU ~]# 
[root@KOU ~]# echo ${riben[rb0]}

[root@KOU ~]# echo ${riben[rb1]}
tokyo
[root@KOU ~]# 
[root@KOU ~]# echo ${riben[rb2]}

[root@KOU ~]# echo ${riben[rb3]}
oosaka
[root@KOU ~]# 

[root@KOU ~]# 
[root@KOU ~]# echo ${#riben[*]}
3
[root@KOU ~]# echo ${#riben[@]}
3
[root@KOU ~]#

**************************************************************************

2、 数组的应用
2.1  数组的长度表示方式(数组的元素个数、数组的所有元素、数组中第一个元素的字符个数)
2.2  数组元素切片
2.3  向数组中追加元素
2.4  删除数组中的某个元素

2.1 数组的长度表示方式:
${#ARRAY_NAME[*]}            表示数组中元素个数;
${#ARRAY_NAME[@]}          表示数组中元素个数;

${#ARRAY_NAME}                 表示数组中第一个元素的字符个数;

${ARRAY_NAME[*]}              表示数组中的所有元素;
${ARRAY_NAME[@]}            表示数组中的所有元素;

[root@KOU ~]# 
[root@KOU ~]# jinrong=([0]="shediao" [3]="yitiantulong" [8]="shengdiao")
[root@KOU ~]# 
[root@KOU ~]# read -a huashan
dongxue xidu nandi beigai wangchongyang
[root@KOU ~]#
查看数组中的元素个数;
-----------------------------
[root@KOU ~]# echo ${#huashan[*]}
5
[root@KOU ~]# echo ${#jinrong[*]}
3
[root@KOU ~]# 
[root@KOU ~]# echo ${#jinrong[@]}
3
[root@KOU ~]# 

查看数组中第一个元素的字符个数;
------------------------------------
[root@KOU ~]# 
[root@KOU ~]# echo ${huashan[0]}
dongxue
[root@KOU ~]# 
[root@KOU ~]# echo ${#huashan}
7
[root@KOU ~]#

查看数组中的所有元素;
--------------------------------------
[root@KOU ~]# 
[root@KOU ~]# echo ${huashan[*]}
dongxue xidu nandi beigai wangchongyang
[root@KOU ~]# 
[root@KOU ~]# echo ${huashan[@]}
dongxue xidu nandi beigai wangchongyang
[root@KOU ~]#

**************************************************

示例1:求10个随机数中最大数与最小数,分别显示出来;

#!/bin/bash
#

declare -a rand
declare -i max=0
declare -i min=65535

for i in {0..9}; do
  rand[$i]=$RANDOM
  echo "${rand[$i]}"
  [ ${rand[$i]} -gt $max ] && max=${rand[$i]}
  [ ${rand[$i]} -lt $min ] && min=${rand[$i]}
done

echo
echo -e "MAX:$max.\nMIN:$min."

----------------------------------------------------------------------------

#!/bin/bash
#

declare -a rand
declare -i max=0
declare -i min=65535

for ((i=0;i<=9;i++)); do
  rand[$i]=$RANDOM
  echo "${rand[$i]}"
  [ ${rand[$i]} -gt $max ] && max=${rand[$i]}
  [ ${rand[$i]} -lt $min ] && min=${rand[$i]}
done

echo
echo -e "MAX:$max.\nMIN:$min."

示例2:定义一个数组,数组中的元素是/var/log/*.log目录下的所有以.log结尾的文件;统计其下标为偶数的文件中的行数之和;

#!/bin/bash
#

declare -a files
files=(/var/log/*.log)

declare -i lines=0

for i in $(seq 0 $[${#files[*]}-1]); do
  if [ $[$i%2] -eq 0 ]; then
    let lines+=$(wc -l ${files[$i]} | cut -d' ' -f1)
  fi
done

echo "lines:$lines."

示例3:生成10个随机数,并由小到大排序(冒泡排序法);

#!/bin/bash
#

declare -i i=1

while [ $i -le 10 ]; do
        if [ $i -eq 1 ]; then
                a[$i]=$RANDOM
        else
                j=$i
                a[$j]=$RANDOM
                while [ $j -ge 2 ] && [ ${a[$j]} -le ${a[$(($j-1))]} ]; do
                        t=${a[$j]}
                        a[$j]=${a[$(($j-1))]}
                        a[$(($j-1))]=$t
                        let j--
                done
        fi
        let i++
done

echo ${a[@]}

2.2  数组元素切片
${ARRAY_NAME[@]:offset:number}
${ARRAY_NAME[*]:offset:number}

offset:偏移量,表示从第一个元素开始计算,要跳过多少个元素;
number:表示要取出多少个元素;省略’number’,则表示偏移量之后的所有元素;

举例:

[root@KOU abc]# 
[root@KOU abc]# file=(/etc/[Pp]*)
[root@KOU abc]# 
[root@KOU abc]# echo ${file[@]}
/etc/PackageKit /etc/pam.d /etc/passwd /etc/passwd- /etc/pbm2ppa.conf /etc/pinforc /etc/pkcs11 /etc/pki /etc/plymouth /etc/pm /etc/pnm2ppa.conf /etc/polkit-1 /etc/popt.d /etc/postfix /etc/ppp /etc/prelink.conf.d /etc/printcap /etc/profile /etc/profile.d /etc/protocols /etc/pulse /etc/purple /etc/python
[root@KOU abc]# 
[root@KOU abc]# echo ${file[@]:2:5}
/etc/passwd /etc/passwd- /etc/pbm2ppa.conf /etc/pinforc /etc/pkcs11
[root@KOU abc]# 
[root@KOU abc]# echo ${file[@]:5:3}
/etc/pinforc /etc/pkcs11 /etc/pki
[root@KOU abc]# 
[root@KOU abc]# echo ${file[@]:3}
/etc/passwd- /etc/pbm2ppa.conf /etc/pinforc /etc/pkcs11 /etc/pki /etc/plymouth /etc/pm /etc/pnm2ppa.conf /etc/polkit-1 /etc/popt.d /etc/postfix /etc/ppp /etc/prelink.conf.d /etc/printcap /etc/profile /etc/profile.d /etc/protocols /etc/pulse /etc/purple /etc/python
[root@KOU abc]#

2.3  向数组中追加元素

向非稀疏格式的数组中追加元素:
ARRAY_NAME[${#ARRAY_NAME[*]}]=VALUE
ARRAY_NAME[${#ARRAY_NAME[@]}]=VALUE

2.4 删除数组中的某个元素

unset  ARRAY_NAME[INDEX]

Leave a Reply

Your email address will not be published. Required fields are marked *