数组

.数组

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

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

索引:编号从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
下一篇 2016-11-23

相关推荐

  • 模块式LAMP和fpm式LAMP实现wordpress

           Linux+Apache+Mysql/MariaDB+Perl/PHP/Python这一组合常用来搭建动态网站或者服务器的开源软件,随着开源潮流的蓬勃发展,开源的LAMP已经与J2EE和.Net商业软件形成三足鼎立之势,并且该软件平台在软件方面的投资成本较低,,LAMP平台已经成为最强…

    Linux干货 2016-04-22
  • Linux基础学习总结(四)

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 cp -r /etc/skel/ /home/tuser1chmod -R 700 /home/tuser1ll -d /home/tuser1 2、编辑/etc/group文件,添加组hadoop。 echo “hadoo…

    Linux干货 2016-10-03
  • Linux软件包安装—-卷一软件包管理rpm方式安装程序

    rpm方式手动安装程序,可能需要”人工智能”的方式手动去处理程序依赖问题

    Linux干货 2017-12-02
  • 深入浅出Docker(一):Docker核心技术预览

    原文链接:http://www.infoq.com/cn/articles/docker-core-technology-preview/ 【编者按】Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上,基于Go语言开发并遵从Apache2.0协议开源。Docker提供了一种在安全、可重复的环境中自…

    2015-04-10
  • 路由配置注意事项

    路由配置注意事项 在把linux主机当做路由的时候,切记把转发功能打开,防火墙关闭,否则到头来一场空。 红脸主机: 黄脸主机: 路由A: [root@localhost network-scripts]# ip route 192.168.240.0/24 via 10.0.0.11 dev eth1 172.16.0.0/16 dev eth0 proto…

    2017-05-03
  • 字符串处理

      一.字符串处理 v bash 的字符串处理工具: (一)字符串切片: ${#var}: 返回字符串变量var 的长度 例: [root@lxc ~]# a="     " [root@lxc ~]# echo ${#a} 5 [root@lxc ~]# ${var:offse…

    Linux干货 2016-11-24