linux中利用脚本编写数组,Linux基础之bash脚本进阶篇-数组(示例代码)

数组

什么是数组?

计算机中的数组指的是具有相同类型的若干变量按照一定的顺序组织起来的一种数据类型。

通俗点来说数组就是变量的一种,按现在的术语来说就是变量圈子中的一个,只不过这个变量圈子都是相同类型的变量并有一定的组织顺序。

数组的格式

array[key]=value

array:表示数组变量的名称

key:表示数组元素的索引又称下标

value:表示key对应的数组元素的值

大体了解了什么是数组,下面就来了解下如何定义数组或如何创建数组

定义数组

定义数组主要有如下几种类型

1、通过指定元素来定义数组#!/bin/bash

#通过指定元素来定义数组

#指定数组元素值

array[0]=8

array[2]=two

#输出数组元素

echo "${array[@]}"

运行脚本[[email protected] test]# sh 20160910-1

8 two

2、通过declare语句来定义数组#!/bin/bash

#通过declare语句来定义数组

#定义数组 -a 选项表示后面定义的是一个名为array的数组。

declare -a array

#为元素赋值

array[0]=1

array[1]=2

#输出元素值

echo "${array[@]}"

运行脚本[[email protected] test]# sh 20160910-2

1 2

通过上面两个例子,其实可以发现在shell中这样的声明不是非常必要的,因为在shell中,所有的变量不必显示定义就可以用作数组,在某种意义上所有变量都是数组:赋值给没有下标的变量与赋值给下标内为0的元素效果是相同的。

示例:#!/bin/bash

tom=1

echo "${tom}"

echo "${tom[0]}"

运行脚本[[email protected] test]# sh 20160910-3

1

1

由结果可知赋值给没有下标的变量与赋值给下标内为0的元素效果是相同的。

3、通过元素列表定义数组#!/bin/bash

#通过元素列表定义数组

#定义数组

array=(1 2 3 4 5 6)

#输出第4个数组元素的值

echo "The fourth element is ${array[3]}"

#输出所有元素的值

echo "The elements of this array are ${array[@]}"

运行脚本[[email protected] test]# sh 20160910-4

The fourth element is 4

The elements of this array are 1 2 3 4 5 6

数组的访问或查看

数组访问有若干途径,下面列举常用两种:

1、通过索引值来查看其对应的元素值#!/bin/bash

#定义数组

array=(1 2 3)

#查看下标为1的元素

echo "the second element is ${array[1]}"

运行脚本[[email protected] test]# sh 20160910-5

the second element is 2

2、通过循环列表遍历数组

我们如果要用for循环的c表达,那么有一个问题需要解决,就是取值范围,而数组中有多少个元素只有定义的人才知道,如果有一个现成的数组,我们在不知道其有多少个元素的情况下如何通过for循环遍历显示?

为此我们需要先知道该数组有多少个元素,在数组里面可以通过${#array[@]}表示#!/bin/bash

#通过循环列表定义数组

#定义数组

array=(one two three four five 6 7 8 9 10)

#获取数组长度

length="${#array[@]}"

#通过循环遍历数组

for ((i=0;i

echo "${array[$i]}"

done

运行脚本[[email protected] test]# sh 20160910-6one

two

three

four

five

6

7

8

9

10

关联数组

“关联数组”是一种具有特殊索引方式的数组。不仅可以通过整数来索引它,还可以使用字符串或者其他类型的值(除了NULL)来索引它

关联数组必须事先声明,否则导致创建关联数组失败

declare -A  array声明关联数组

array=([a]=tom [b]=john [c]=3)

如果我们定义下标内是人名其对应的元素为成绩,那么输入指定的人名就可以知道其对应的成绩#!/bin/bash

#定义关联数组

declare -A grades

#为数组下标指定对应的分数

grades=([tom]=90 [lilei]=88 [rose]=92 [Lucy]=80)

#给一个名字

read -p "Give a name : " name_G

#判断是否给了内容,若没有则在提示后退出

[ -z $name_G ] && echo "None please select [tom|lilei|rose|Lucy] && exit 1"

#根据输入的名字输出对应的分数

case $name_G in

tom)

echo "tom‘s grades is ${grades[tom]}";;

lilei)

echo "lilei‘s grades is ${grades[lilei]}";;

rose)

echo "rose‘s grades is ${grades[rose]}";;

Lucy)

echo "Lucy‘s grades is ${grades[Lucy]}";;

*)

echo "please select [tom|lilei|rose|Lucy]"

esac

运行脚本[[email protected] test]# sh 20160910-7

Give a name : tom

Tom‘s grades is 90

[[email protected] test]# vim 20160910-7

[[email protected] test]# sh 20160910-7

Give a name : rose

rose‘s grades is 92

[[email protected] test]# sh 20160910-7

Give a name : lilei

lilei‘s grades is 88

[[email protected] test]# sh 20160910-7

Give a name : Lucy

Lucy‘s grades is 80

[[email protected] test]# sh 20160910-7

Give a name : other

please select [tom|lilei|rose|Lucy]

数组的增删合

增加数组个数#!/bin/bash

#定义数组

array=(1 2)

#显示数组所有元素

echo "${array[@]}"

#显示数组元素个数

echo "${#array[@]}"

#追加数组元素

array[2]=3

array[3]=4

#显示追加后的数组信息

echo "${array[@]}"

echo "${#array[@]}"

运行脚本[[email protected] test]# sh 20160910-8

1 2

2

1 2 3 4

4

可以从数组的任意未使用的下标数开始。可以非连续增加数组

删除数组

1、删除数组指定某个元素

命令:unset array[n]#!/bin/bash

#定义数组

array=(1 2 3)

#显示数组所有元素

echo "${array[@]}"

#显示下标为0的数组元素

echo "${array[0]}"

#删除下标为0的数组元素

unset array[0]

echo "${array[@]}"

echo "${array[0]}"

2、删除整个数组

命令:unset array#!/bin/bash

#定义数组

array=(1 2 3)

echo "${array[@]}"

#删除整个数组

unset array

echo "${array[@]}"

运行脚本[[email protected] test]# sh 20160910-9

1 2 3

数组之间的合并

数组可以将多个不同的数组组合成一个新的数组。

数组连接语法如下:

("${array1[@]}" "${array2[@]}" ... "${arrayN[@]}")

数组之间用空格隔开

示例#定义两个数组

array1=(1 2 3)

array2=(4 5 6)

#连接数组

array1_2=("${array1[@]}" "${array2[@]}")

#显示合并后数组的所有元素

echo "${array1_2[@]}"

运行脚本[[email protected] test]# sh 20160910-10

1 2 3 4 5 6

数组中的切片

所谓切片,是指截取数组的部分元素或某个元素的部分内容

基本语法为:${array[@|*|n]:start:length}

示例1:给定一个长度为8的数组,截取第3个元素开始的4个元素。#!/bin/bash

array=(1 2 3 4 5 6 7 8)

var=${array[@]:3:4}

echo "$var"

运行脚本[[email protected] test]# sh 20160910-11

4 5 6 7

示例2:给定一个长度为3的数组,截取第2个元素,从该元素的第1个字符开始,截取其中3个字符#!/bin/bash

array=(apple orange banana)

var=${array[2]:1:3}

echo "$var"

运行脚本[[email protected] test]# sh 20160910-12

ana

数组中的替换

数组的替换是指将某个数组元素的部分内容用其他字符串来代替,但不影响原来的数组的值。

基本语法为:${array[@|*]/pattern/replacement}#!/bin/bash

replace=(a b c d e)

echo "the replaced array is ${replaced[@]/c/3}"

echo "the original array is ${replace[@]}"

运行脚本[[email protected] test]# sh 20160910-13

the replaced array is a b 3 d e

the original array is a b c d e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值