7 数组
7.1 数组介绍
变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合
数组名和索引
- 索引的编号从0开始,属于数值索引
- 索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash4.0版本之后开始支持
- bash的数组支持稀疏格式(索引不连续)
7.2 声明数组
#普通数组可以不事先声明,直接使用
declare -a ARRAY_NAME
#关联数组必须先声明,再使用
declare -A ARRAY_NAME
declare -a ARRAY_NAME
#关联数组必须先声明,再使用
declare -A ARRAY_NAME
1
2
3
4
|
#普通数组可以不事先声明,直接使用
declare - a ARRAY_NAME
#关联数组必须先声明,再使用
declare - A ARRAY_NAME
|
注意:两者不可相互转换
7.3 数组赋值
数组元素的赋值 (1) 一次只赋值一个元素
ARRAY_NAME[INDEX]=VALUE
1
|
ARRAY_NAME [ INDEX ] = VALUE
|
范例:
weekdays[0]="Sunday"
weekdays[4]="Thursday"
weekdays[4]="Thursday"
1
2
|
weekdays [ 0 ] = "Sunday"
weekdays [ 4 ] = "Thursday"
|
(2) 一次赋值全部元素
ARRAY_NAME=("VAL1" "VAL2" "VAL3" ...)
1
|
ARRAY_NAME = ( "VAL1" "VAL2" "VAL3" . . . )
|
范例:
title=("ceo" "coo" "cto")
num=({0..10})
alpha=({a..g})
file=( *.sh )
num=({0..10})
alpha=({a..g})
file=( *.sh )
1
2
3
4
|
title = ( "ceo" "coo" "cto" )
num = ( { 0..10 } )
alpha = ( { a . . g } )
file = ( * . sh )
|
(3) 只赋值特定元素
ARRAY_NAME=([0]="VAL1" [3]="VAL2" ...)
1
|
ARRAY_NAME = ( [ 0 ] = "VAL1" [ 3 ] = "VAL2" . . . )
|
(4) 交互式数组值对赋值
read -a ARRAY
1
|
read - a ARRAY
|
范例:
[root@centos8 ~]#declare -A course
[root@centos8 ~]#declare -a course
-bash: declare: course: cannot convert associative to indexed array
[root@centos8 ~]#file=( *.sh )
[root@centos8 ~]#declare -A file
-bash: declare: file: cannot convert indexed to associative array
[root@centos8 ~]#declare -a course
-bash: declare: course: cannot convert associative to indexed array
[root@centos8 ~]#file=( *.sh )
[root@centos8 ~]#declare -A file
-bash: declare: file: cannot convert indexed to associative array
1
2
3
4
5
6
|
[ root @ centos8 ~ ] #declare -A course
[ root @ centos8 ~ ] #declare -a course
- bash : declare : course : cannot convert associative to indexed array
[ root @ centos8 ~ ] #file=( *.sh )
[ root @ centos8 ~ ] #declare -A file
- bash : declare : file : cannot convert indexed to associative array
|
7.4 显示所有数组
显示所有数组:
declare -a
1
|
declare - a
|
范例:
[root@centos8 ~]#declare -a
declare -a BASH_ARGC=()
declare -a BASH_ARGV=()
declare -a BASH_COMPLETION_VERSINFO=([0]="2" [1]="7")
declare -a BASH_LINENO=()
declare -ar BASH_REMATCH=()
declare -a BASH_SOURCE=()
declare -ar BASH_VERSINFO=([0]="4" [1]="4" [2]="19" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
declare -a DIRSTACK=()
declare -a FUNCNAME
declare -a GROUPS=()
declare -a PIPESTATUS=([0]="0")
declare -a BASH_ARGC=()
declare -a BASH_ARGV=()
declare -a BASH_COMPLETION_VERSINFO=([0]="2" [1]="7")
declare -a BASH_LINENO=()
declare -ar BASH_REMATCH=()
declare -a BASH_SOURCE=()
declare -ar BASH_VERSINFO=([0]="4" [1]="4" [2]="19" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
declare -a DIRSTACK=()
declare -a FUNCNAME
declare -a GROUPS=()
declare -a PIPESTATUS=([0]="0")
1
2
3
4
5
6
7
8
9
10
11
12
|
[ root @ centos8 ~ ] #declare -a
declare - a BASH_ARGC = ( )
declare - a BASH_ARGV = ( )
declare - a BASH_COMPLETION_VERSINFO = ( [ 0 ] = "2" [ 1 ] = "7" )
declare - a BASH_LINENO = ( )
declare - ar BASH_REMATCH = ( )
declare - a BASH_SOURCE = ( )
declare - ar BASH_VERSINFO = ( [ 0 ] = "4" [ 1 ] = "4" [ 2 ] = "19" [ 3 ] = "1" [ 4 ] = "release" [ 5 ] = "x86_64-redhat-linux-gnu" )
declare - a DIRSTACK = ( )
declare - a FUNCNAME
declare - a GROUPS = ( )
declare - a PIPESTATUS = ( [ 0 ] = "0" )
|
7.5 引用数组
引用数组元素
${ARRAY_NAME[INDEX]}
#如果省略[INDEX]表示引用下标为0的元素
#如果省略[INDEX]表示引用下标为0的元素
1
2
|
$ { ARRAY_NAME [ INDEX ] }
#如果省略[INDEX]表示引用下标为0的元素
|
范例:
[root@centos8 ~]#declare -a title=([0]="ceo" [1]="coo" [2]="cto")
[root@centos8 ~]#echo ${title[1]}
coo
[root@centos8 ~]#echo ${title}
ceo
[root@centos8 ~]#echo ${title[2]}
cto
[root@centos8 ~]#echo ${title[3]}
[root@centos8 ~]#echo ${title[1]}
coo
[root@centos8 ~]#echo ${title}
ceo
[root@centos8 ~]#echo ${title[2]}
cto
[root@centos8 ~]#echo ${title[3]}
1
2
3
4
5
6
7
8
|
[ root @ centos8 ~ ] #declare -a title=([0]="ceo" [1]="coo" [2]="cto")
[ root @ centos8 ~ ] #echo ${title[1]}
coo
[ root @ centos8 ~ ] #echo ${title}
ceo
[ root @ centos8 ~ ] #echo ${title[2]}
cto
[ root @ centos8 ~ ] #echo ${title[3]}
|
引用数组所有元素
${ARRAY_NAME[*]}
${ARRAY_NAME[@]}
${ARRAY_NAME[@]}
1
2
|
$ { ARRAY_NAME [ * ] }
$ { ARRAY_NAME [ @ ] }
|
范例:
[root@centos8 ~]#echo ${title[@]}
ceo coo cto
[root@centos8 ~]#echo ${title[*]}
ceo coo cto
ceo coo cto
[root@centos8 ~]#echo ${title[*]}
ceo coo cto
1
2
3
4
|
[ root @ centos8 ~ ] #echo ${title[@]}
ceo coo cto
[ root @ centos8 ~ ] #echo ${title[*]}
ceo coo cto
|
数组的长度,即数组中元素的个数
${#ARRAY_NAME[*]}
${#ARRAY_NAME[@]}
${#ARRAY_NAME[@]}
1
2
|
$ { #ARRAY_NAME[*]}
$ { #ARRAY_NAME[@]}
|
范例:
[root@centos8 ~]#echo ${#title[*]}
3
3
1
2
|
[ root @ centos8 ~ ] #echo ${#title[*]}
3
|
7.6 删除数组
删除数组中的某元素,会导致稀疏格式
unset ARRAY[INDEX]
1
|
unset ARRAY [ INDEX ]
|
[root@centos8 ~]#echo ${title[*]}
ceo coo cto
[root@centos8 ~]#unset title[1]
[root@centos8 ~]#echo ${title[*]}
ceo cto
ceo coo cto
[root@centos8 ~]#unset title[1]
[root@centos8 ~]#echo ${title[*]}
ceo cto
1
2
3
4
5
|
[ root @ centos8 ~ ] #echo ${title[*]}
ceo coo cto
[ root @ centos8 ~ ] #unset title[1]
[ root @ centos8 ~ ] #echo ${title[*]}
ceo cto
|
删除整个数组
unset ARRAY
1
|
unset ARRAY
|
范例:
[root@centos8 ~]#unset title
[root@centos8 ~]#echo ${title[*]}
[root@centos8 ~]#
[root@centos8 ~]#echo ${title[*]}
[root@centos8 ~]#
1
2
3
4
|
[ root @ centos8 ~ ] #unset title
[ root @ centos8 ~ ] #echo ${title[*]}
[ root @ centos8 ~ ] #
|
7.7 数组数据处理
数组切片:
${ARRAY[@]:offset:number}
offset #要跳过的元素个数
number #要取出的元素个数
#取偏移量之后的所有元素
{ARRAY[@]:offset}
offset #要跳过的元素个数
number #要取出的元素个数
#取偏移量之后的所有元素
{ARRAY[@]:offset}
1
2
3
4
5
|
$ { ARRAY [ @ ] : offset : number }
offset #要跳过的元素个数
number #要取出的元素个数
#取偏移量之后的所有元素
{ ARRAY [ @ ] : offset }
|
范例:
[root@centos8 ~]#num=({0..10})
[root@centos8 ~]#echo ${num[*]:2:3}
2 3 4
[root@centos8 ~]#echo ${num[*]:6}
6 7 8 9 10
[root@centos8 ~]#echo ${num[*]:2:3}
2 3 4
[root@centos8 ~]#echo ${num[*]:6}
6 7 8 9 10
1
2
3
4
5
|
[ root @ centos8 ~ ] #num=({0..10})
[ root @ centos8 ~ ] #echo ${num[*]:2:3}
2 3 4
[ root @ centos8 ~ ] #echo ${num[*]:6}
6 7 8 9 10
|
向数组中追加元素:
ARRAY[${#ARRAY[*]}]=value
ARRAY[${#ARRAY[@]}]=value
ARRAY[${#ARRAY[@]}]=value
1
2
|
ARRAY [ $ { # ARRAY [ * ] } ] = value
ARRAY [ $ { # ARRAY [ @ ] } ] = value
|
范例:
[root@centos8 ~]#num[${#num[@]}]=11
[root@centos8 ~]#echo ${#num[@]}
12
[root@centos8 ~]#echo ${num[@]}
0 1 2 3 4 5 6 7 8 9 10 11
[root@centos8 ~]#echo ${#num[@]}
12
[root@centos8 ~]#echo ${num[@]}
0 1 2 3 4 5 6 7 8 9 10 11
1
2
3
4
5
|
[ root @ centos8 ~ ] #num[${#num[@]}]=11
[ root @ centos8 ~ ] #echo ${#num[@]}
12
[ root @ centos8 ~ ] #echo ${num[@]}
0 1 2 3 4 5 6 7 8 9 10 11
|
7.8 关联数组
declare -A ARRAY_NAME
ARRAY_NAME=([idx_name1]='val1' [idx_name2]='val2‘...)
ARRAY_NAME=([idx_name1]='val1' [idx_name2]='val2‘...)
1
2
|
declare - A ARRAY_NAME
ARRAY_NAME = ( [ idx_name1 ] = 'val1' [ idx_name2 ] =' val2‘ . . . )
|
注意:关联数组必须先声明再调用
范例:
[root@centos8 ~]#name[ceo]=mage
[root@centos8 ~]#name[cto]=wang
[root@centos8 ~]#name[coo]=zhang
[root@centos8 ~]#echo ${name[ceo]}
zhang
[root@centos8 ~]#echo ${name[cto]}
zhang
[root@centos8 ~]#echo ${name[coo]}
zhang
[root@centos8 ~]#echo ${name}
zhang
[root@centos8 ~]#declare -A name
-bash: declare: name: cannot convert indexed to associative array
[root@centos8 ~]#unset name
[root@centos8 ~]#declare -A name
[root@centos8 ~]#name[ceo]=mage
[root@centos8 ~]#name[cto]=wang
[root@centos8 ~]#name[coo]=zhang
[root@centos8 ~]#echo ${name[coo]}
zhang
[root@centos8 ~]#echo ${name[ceo]}
mage
[root@centos8 ~]#echo ${name[cto]}
wang
[root@centos8 ~]#echo ${name[*]}
mage wang zhang
[root@centos8 ~]#name[cto]=wang
[root@centos8 ~]#name[coo]=zhang
[root@centos8 ~]#echo ${name[ceo]}
zhang
[root@centos8 ~]#echo ${name[cto]}
zhang
[root@centos8 ~]#echo ${name[coo]}
zhang
[root@centos8 ~]#echo ${name}
zhang
[root@centos8 ~]#declare -A name
-bash: declare: name: cannot convert indexed to associative array
[root@centos8 ~]#unset name
[root@centos8 ~]#declare -A name
[root@centos8 ~]#name[ceo]=mage
[root@centos8 ~]#name[cto]=wang
[root@centos8 ~]#name[coo]=zhang
[root@centos8 ~]#echo ${name[coo]}
zhang
[root@centos8 ~]#echo ${name[ceo]}
mage
[root@centos8 ~]#echo ${name[cto]}
wang
[root@centos8 ~]#echo ${name[*]}
mage wang zhang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[ root @ centos8 ~ ] #name[ceo]=mage
[ root @ centos8 ~ ] #name[cto]=wang
[ root @ centos8 ~ ] #name[coo]=zhang
[ root @ centos8 ~ ] #echo ${name[ceo]}
zhang
[ root @ centos8 ~ ] #echo ${name[cto]}
zhang
[ root @ centos8 ~ ] #echo ${name[coo]}
zhang
[ root @ centos8 ~ ] #echo ${name}
zhang
[ root @ centos8 ~ ] #declare -A name
- bash : declare : name : cannot convert indexed to associative array
[ root @ centos8 ~ ] #unset name
[ root @ centos8 ~ ] #declare -A name
[ root @ centos8 ~ ] #name[ceo]=mage
[ root @ centos8 ~ ] #name[cto]=wang
[ root @ centos8 ~ ] #name[coo]=zhang
[ root @ centos8 ~ ] #echo ${name[coo]}
zhang
[ root @ centos8 ~ ] #echo ${name[ceo]}
mage
[ root @ centos8 ~ ] #echo ${name[cto]}
wang
[ root @ centos8 ~ ] #echo ${name[*]}
mage wang zhang
|
7.9 范例
范例:生成10个随机数保存于数组中,并找出其最大值和最小值
#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]}&& 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
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]}&& 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
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/bin/bash
declare - i min max
declare - a nums
for ( ( i = 0 ; i < 10 ; i ++ ) ) ; do
nums [ $i ] = $RANDOM
[ $i - eq 0 ] && min = $ { nums [ 0 ] } && max = $ { nums [ 0 ] } && 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
|
范例:编写脚本,定义一个数组,数组中的元素对应的值是/var/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"
#
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"
1
2
3
4
5
6
7
8
9
10
11
|
#!/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"
|
练习
-
输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
-
将下图所示,实现转置矩阵 matrix.sh 1 2 3 1 4 7 4 5 6 ===> 2 5 8 7 8 9 3 6 9
-
打印杨辉三角形
本文链接:http://www.3yyy.top/34350.html