数组

.数组

变量:存储单个元素的内存空间

数组:存储多个元素的连续的内存空间,相当于多个变量的集合。

索引:编号从0 开始,属于数值索引

 注:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash4.0 版本之后开始支持,bash 的数组支持稀疏格式(索引不连续)

.数组的声明

declare -a ARRAY_NAME

declare -A ARRAY_NAME:  关联数组

例:

[root@lxc ~]# declare -a week

[root@lxc ~]# declare -A char

 

.赋值与引用

赋值:

(1) 一次只赋值一个元素;

ARRAY_NAME[INDEX]=VALUE

例:

[root@lxc ~]# declare -a week

[root@lxc ~]# week[0]="sun"

[root@lxc ~]# week[1]="mon"

[root@lxc ~]# week[6]="sat"

 

(2) 一次赋值全部元素:

ARRAY_NAME=("VAL1" "VAL2" "VAL3" …)

例:

[root@lxc ~]# declare -a name

[root@lxc ~]# name=("lxc" "xyh" "hy")

 

(3) 只赋值特定元素:

ARRAY_NAME=([0]="VAL1" [3]="VAL2" …)

例:

[root@lxc ~]# declare -a menu

[root@lxc ~]# menu=([1]="chaofan" [2]="chaobing" [3]="chaofen")

 (4)  交互式数组值对赋值

read -a ARRAY

例:

[root@lxc ~]# read -a title

boss  ceo  cto

[root@lxc ~]#

 

引用数组:

 引用数组元素:${ARRAY_NAME[INDEX]}

例:

[root@lxc ~]# echo ${week[1]}

mon

[root@lxc ~]# echo ${week[0]}

sun

[root@lxc ~]# echo ${week[6]}

sat

[root@lxc ~]#

注:省略[INDEX] 表示引用下标为0 的元素

 

数组的长度( 数组中元素的个数)

${#ARRAY_NAME[*]}

${#ARRAY_NAME[@]}

例:

[root@lxc ~]# echo ${title[*]}

boss ceo cto

[root@lxc ~]# echo ${title[@]}

boss ceo cto

[root@lxc ~]#

实例:

随机生成10个数保存到数组中,并取出最大值和最小值

[root@lxc ~]# vim random_max_or_min.sh

 

#!/bin/bash

#this is judge max and min of random

declare -a rand

declare -a max

declare -a min

num=10

for i in `seq 0 $[num-1]`

do

        rand[$i]=$RANDOM

        [ $i -eq 0  ] && max=${rand[0]} && min=${rand[0]}

        [ ${rand[$i]} -gt $max ] && max=${rand[$i]}

        [ ${rand[$i]} -lt $min ] && min=${rand[$i]}

done

echo "all random are ${rand[*]}"

echo "max: $max"

echo "min: $min"

"random_max_or_min.sh" 16L, 362C

 

[root@lxc ~]# chmod +x random_max_or_min.sh

[root@lxc ~]# ./random_max_or_min.sh

all random are 24538 32455 31167 6683 6822 3624 7588 8009 3007 17035

max: 32455

min: 3007

[root@lxc ~]#

 

.数组数据处理

引用数组中的元素:

所有元素:${ARRAY[@]}, ${ARRAY[*]}

例:

[root@lxc ~]# echo ${week[*]}

sun mon sat

[root@lxc ~]# echo ${menu[@]}

chaofan chaobing chaofen

数组切片:${ARRAY[@]:offset:number}

offset:  要跳过的元素个数

number:  要取出的元素个数

取偏移量之后的所有元素

${ARRAY[@]:offset}

例:

[root@lxc ~]# num=({1..10})

[root@lxc ~]# echo ${num[@]}

1 2 3 4 5 6 7 8 9 10

[root@lxc ~]# echo ${num[@]:3:4}

4 5 6 7

[root@lxc ~]#

向数组中追加元素:

ARRAY[${#ARRAY[*]}]

例:

[root@lxc ~]# num=({1..10})

[root@lxc ~]# echo ${num[@]}

1 2 3 4 5 6 7 8 9 10

[root@lxc ~]# num[${#num[@]}]=11

[root@lxc ~]# echo ${num[@]}

1 2 3 4 5 6 7 8 9 10 11

[root@lxc ~]#

删除数组中的某元素:导致稀疏格式

unset ARRAY[INDEX]

[root@lxc ~]# echo ${num[@]}

1 2 3 4 5 6 7 8 9 10 11

[root@lxc ~]# unset num[0]

[root@lxc ~]# echo ${num[@]}

2 3 4 5 6 7 8 9 10 11

[root@lxc ~]#

关联数组:

declare -A ARRAY_NAME  注意:必须先声明,再调用

ARRAY_NAME=([idx_name1]='val1' [idx_name2]='val2…)

例:

[root@lxc ~]# declare  -A n

[root@lxc ~]# n=([w]=wang [s]=sun [b]=bai )

[root@lxc ~]# echo ${n[@]}

bai sun wang

[root@lxc ~]#

                                                 

原创文章,作者:changge,如若转载,请注明出处:http://www.178linux.com/60575

(0)
changgechangge
上一篇 2016-11-23 23:08
下一篇 2016-11-23 23:21

相关推荐

  • 从Linux小白到大牛——与狼共舞的日子10

    马哥教育网络班21期+第10周课程练习 1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) POST –> Boot Sequence(BIOS) –> Boot Loader(MBR)   –> Kernel+ramdi…

    Linux干货 2016-12-05
  • 关于 磁盘、文件系统管理

                   磁盘、文件系统管理               1  设备识别2  设备分区3 …

    系统运维 2016-08-30
  • 8.1作业习题

    1,创建testuser uid 1234,主组:bin,辅助组:root,ftp,shell:/bin/csh home:/testdir/testuser useradd -u 1234 -g bin -G root,ftp -s /bin/csh -d /testdir/testuser testuser 2.修改testuser uid:4321,主…

    Linux干货 2016-08-04
  • 运行级别

    运行级别(Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式。运行级别通常分为7等,分别是从0到6,但如果必要的话也可以更多。 例如在大多数Linux操作系统下一共有如下7个典型的运行级别: 0 停机,关机 1 单用户,无网络连接,不运行守护进程,不允许非超级用户登录 2 多用户,无网络连接,不运行守护进程 3 多用户,正常启…

    Linux干货 2017-07-10
  • 磁盘管理进阶

    1、/etc/fstab文件     设备名 挂载点 文件系统 挂载选项 转储频率 自检次序     UUID=e79e4c9d-8d0f-4675-8945-9ec23ea77c67 /             &nb…

    Linux干货 2016-09-02
  • 20本最好的Linux免费书籍

    前些天Neo推荐了一个网站有《超过100本的linux免费书籍》,这里,我也向大家推荐20本最好的Linux免费书籍,当然,也是英文版的。 1. Ubuntu Pocket Guide and Reference 一本介绍关于Ubuntu 8.04和8.10的使用书。 Website www.ubuntupocketguide.com Author Keir…

    Linux干货 2015-04-01