描述:
变量是存储单个元素的内存空间,而数组是多个变量的集合,是一个连续的空间;但整个数组只能有
的名字。
数组内的数据都有指定的索引,从而找到数组内所指定的数据。索引的编号是从0开始的,依次递增(0,1,2,3,…),这种方式叫数值索引。格式为:数组名[索引];${ARRAY_NAME[INDEX]}。索引也支持自定义的格式,也被称为关联索引,在bash4.0版本后开始支持。bash的数组也支持稀疏格式,即索引不连续(0,2,4,7,8,…)
用法:
1,使用数组需事先声明,格式如下:
declare -a ARRAY_NAME (声明索引数组)
declare -A ARRAY_NAME (声明关联数组)
2,数组中元素的赋值方式:
1),一次只赋值一个元素
ARRAY_NAME[INDEX]=value
[root@localhost ~]# weekdays[0]="Sunday" [root@localhost ~]# echo ${weekdays[0]} Sunday
2),一次赋值全部元素:
ARRAY_NAME=("VAL1" "VAL2" "VAL3"…)
[root@localhost ~]# weekdays=("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" ) [root@localhost ~]# echo ${weekdays[6]} Saturday [root@localhost ~]# echo ${weekdays[5]} Friday [root@localhost ~]# echo ${weekdays[0]} 第一个元素的索引号是从0开始编号的 Sunday
3),只赋值特定元素
ARRAY_NAME=([0]="VAL1" [3]="VAL4"…)
[root@localhost ~]# weekdays=([0]="Sunday" [2]="Tuesday" [5]="Saturday") [root@localhost ~]# echo ${weekdays[0]} Sunday [root@localhost ~]# echo ${weekdays[2]} Tuesday [root@localhost ~]# echo ${weekdays[5]} Saturday
4),read -a ARRAY_NAME
[root@localhost ~]# read -a number 123 456 789
3,数组的引用
引用数组中的元素:${ARRAY_NAME[INDEX]}
注意:引用时,只给数组名,表示引用下标为0的元素
${ARRY_NAME[*]}
${ARRY_NAME[@]}
数组的长度:(数组中元素的个数)
${#ARRY_NAME[*]}
${#ARRY_NAME[@]}
[root@localhost ~]# weekdays=("Sunday" "Tuesday" "Saturday") [root@localhost ~]# echo ${weekdays[0]} Sunday [root@localhost ~]# echo ${weekdays[*]} Sunday Tuesday Saturday [root@localhost ~]# echo ${weekdays[@]} Sunday Tuesday Saturday [root@localhost ~]# echo ${#weekdays[@]} 3 [root@localhost ~]# echo ${#weekdays[*]} 3
示例:
1,随机生成10个数,并找出其中的最大值和最小值
[root@localhost ~]# cat sum.sh #!/bin/bash declare -a rand declare -i max declare -i min for i in {0..9};do rand[$i]=$RANDOM echo ${rand[$i]} if [ $i -eq 0 ];then max=${rand[$i]} min=${rand[$i]} else [ "$max" -lt "${rand[$i]}" ] && max=${rand[$i]} [ "$min" -gt "${rand[$i]}" ] && min=${rand[$i]} fi done echo "MAX:$max" echo "MIN:$min" [root@localhost ~]# bash sum.sh 21586 1730 19008 16138 17576 3909 206 27706 3824 11997 MAX:27706 MIN:206
处理数组数据:
语法 |
描述 | |||||||
${!array[*]} |
列出所有元素所对应的索引号 | |||||||
${!array[@]} | 列出所有元素所对应的索引号 | |||||||
${array[*]} | 列出关联数组的所有元素 | |||||||
${array[@]} | 列出关联数组的所有元素 | |||||||
${#[array[*]} | 关联数组的长度(元素个数) | |||||||
${#[array[@]} | 关联数组的长度(元素个数) |
示例:
设置数组:
[root@localhost ~]# weekdays=("Sunday" "Tuesday" "Saturday")
1,列出数组中所有元素所对应的索引号
[root@localhost ~]# echo ${!weekdays[*]} 0 1 2 [root@localhost ~]# echo ${!weekdays[@]} 0 1 2
2,列出数组中所有的元素
[root@localhost ~]# echo ${weekdays[@]} Sunday Tuesday Saturday [root@localhost ~]# echo ${weekdays[*]} Sunday Tuesday Saturda
3,显示数组中元素的个数
[root@localhost ~]# echo ${#weekdays[*]} 3 [root@localhost ~]# echo ${#weekdays[@]} 3
数组切片:
格式为:${ARRY_NAME[@]:offset:number}
offset:要路过的元素的个数
number:要取出的元素个数
${ARRAY_NAME[@]:offset}:取偏移量之后的所有元素
示例:
[root@localhost ~]# weekdays=("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday") [root@localhost ~]# echo ${weekdays[@]:3:2} Wednesday Thursday [root@localhost ~]# echo ${weekdays[@]:2} Tuesday Wednesday Thursday Friday Saturday
向非稀疏格式数组中追加元素:
ARRAY_NAME[${#ARRAY_NAME[*]}]=
[root@localhost ~]# number=(2 4 6 8) [root@localhost ~]# echo ${number[*]} 2 4 6 8 [root@localhost ~]# number[${#number[*]}]=10 [root@localhost ~]# echo ${number[*]} 2 4 6 8 10
删除数组中的某元素:
unset ARRAY[INDEX]
[root@localhost ~]# unset number[3] [root@localhost ~]# echo ${number[*]} 2 4 6 10
关联数组:
declare -A ARRAY_NAME
ARRAY_NAME=([index_name1]="value1" [index_name2]="value2"…)
[root@localhost ~]# declare -A num [root@localhost ~]# num=([jishu]="1 3 5" [oushu]="2 4 6 8") [root@localhost ~]# echo ${num[jishu]} 1 3 5 [root@localhost ~]# echo ${num[oushu]} 2 4 6 8
示例:
输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
[root@localhost ~]# cat maopao.sh #!/bin/bash #sorting following array echo "Please input a number list: " read -a arr for ((i=0;i<${#arr[@]};i++));do for ((j=${#arr[@]}-1;j>i;j--));do #echo $j if [[ ${arr[j]} -lt ${arr[j-1]} ]];then t=${arr[j]} arr[j]=${arr[j-1]} arr[j-1]=$t fi done done echo "after sorting: " echo ${arr[@]} [root@localhost ~]# bash maopao.sh Please input a number list: 5 1 4 2 3 0 after sorting: 0 1 2 3 4 5
原创文章,作者:pingsky,如若转载,请注明出处:http://www.178linux.com/41225