数组

一.数组

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

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

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

相关推荐

  • LVM 逻辑卷管理器-缩小LV容量及快照区的建立

    LVM 逻辑卷管理器-缩小LV容量 将/dev/sdb2分区抽离出来,缩小LV容量 (1)首先查看/dev/sdb2的容量大小 [root@centos6 ~]# df -h /mnt/lvm/ Filesystem        &nbs…

    Linux干货 2016-09-19
  • Nagios

    一、Nagios简介   Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。 Nagios原名为NetSaint,由Ethan Galstad开发并维护至今…

    2017-12-20
  • shell脚本2

    使用read命令来接受输入 使用read来把输入值分配给一个或多个shell变量 read后面跟变量,回车输入内容,就可以把输入的内容赋给变量 -p指定要显示的提示 -s 静默输入,一般用于密码 输入的内容不显示 -n N 指定输入的字符长度N 当输入的内容达到了5个字符会直接结束输入 -d‘字符’ 输入结束符 把!作为了结束符,在输入内容时,当输入!会直接…

    2017-08-11
  • TCP常见问题总结

    TCP协议和UDP协议的区别是什么 TCP协议是有连接的,有连接的意思是开始传输实际数据之前TCP的客户端和服务器端必须通过三次握手建立连接,会话结束之后也要结束连接。而UDP是无连接的 TCP协议保证数据按序发送,按序到达,提供超时重传来保证可靠性,但是UDP不保证按序到达,甚至不保证到达,只是努力交付,即便是按序发送的序列,也不保证按序送到。 TCP协议…

    Linux干货 2017-09-02
  • bash编程之数组和字符串处理

    数组: 程序=指令+数据          指令:     数据:变量、文件 数组:存储过个元素的连续的内存空间; 变量:存储单个元素的内存空间; 数组名:整个数组只有一个名字; 数组索引:编号从0开始;   &nbsp…

    Linux干货 2016-08-24
  • 使用yum源码编译安装程序

    需要的安装包:httpd-2.2.29.tar.bz2 1、安装develpment tools     yum groupinstall "Development Tools" 2、下载服务器中的http源安装包到本地,然后解压到指定文件夹中 lftp 10.1.0.1:/pub/Sources/…

    Linux干货 2016-08-24