数组

.数组

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

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

索引:编号从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

相关推荐

  • N26_第一周作业

    一、描述计算机的组成及其功能。 计算机系统:是由硬件(Hardware)系统和软件(Software)系统两大部分构成。 1、硬件系统: 1)控制器(Control):是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算机各部分工作及内存与外设的访问等。2)运算器(Datapath):运算器的功能是…

    2017-02-20
  • 网络N23期第四周grep

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -R /etc/skel /home/tuser1 && chmod g-r,o-r /home/tuser1 [root@localhost ~]# ls -l…

    系统运维 2016-12-05
  • 用户权限

    一、用户和组的主要配置文件
    二、用户管理命令
    三、组管理命令
    四、查看用户相关的ID信息
    五、切换用户或以其他用户身份执行命令

    2018-03-13
  • 3、文本处理命令、用户与组命令练习

    1.列出当前系统上所有已登录用户的用户名,同一个用户登录,则只显示一次 [root@localhost ~]# who root     tty1         2016-08-23 06…

    Linux干货 2016-09-19
  • Homework week-5 grep及find

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; grep -E "^[[:space:]]+" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; grep&nbsp…

    Linux干货 2016-09-06