bash数组

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:82

[color=rgb(85, 85, 85)][font=宋体, "]bash中的数组仅支持一维数组,数组定义好后数组的长度也可以动态地改变。
[color=rgb(85, 85, 85)][font=宋体, "]bash中的数组有两种,一种是常见的索引数组,另一种是bash4.0后支持的关联数组。
[color=rgb(85, 85, 85)][font=宋体, "]先简单谈一下关联数组:[color=rgb(85, 85, 85)][font=宋体, "]declare -A array #申明关联数组,如果要把array当做关联数组使用,这个必须申明[color=rgb(85, 85, 85)][font=宋体, "]array[hello]=red;array[hi]=yellow;array[honey]=green.......
[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em]6
[align=right][size=1em]7
[align=right][size=1em][size=1em][baby@beibei ~]$ declare -A array
[size=1em][baby@beibei ~]$ array[hello]=red
[size=1em][baby@beibei ~]$ array[hi]=yellow
[size=1em][baby@beibei ~]$ echo ${array[hello]}
[size=1em]red
[size=1em][baby@beibei ~]$ echo ${array[hi]}
[size=1em]yellow

[color=rgb(85, 85, 85)][font=宋体, "]关联数组用的不多,下面介绍我们常用的[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]申明数组
[color=rgb(85, 85, 85)][font=宋体, "]declare -a array[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]数组的赋值[color=rgb(85, 85, 85)][font=宋体, "]1)一次对一个元素赋值:[color=rgb(85, 85, 85)][font=宋体, "] array[0]=red[color=rgb(85, 85, 85)][font=宋体, "] array[1]=yellow[color=rgb(85, 85, 85)][font=宋体, "] ......[color=rgb(85, 85, 85)][font=宋体, "] 2)一次对所有元素进行赋值[color=rgb(85, 85, 85)][font=宋体, "] array=(red yellow green blue)[color=rgb(85, 85, 85)][font=宋体, "] 3)按索引赋值[color=rgb(85, 85, 85)][font=宋体, "] array=([2]=green [1]=red [3]=yellow [0]=blue)[color=rgb(85, 85, 85)][font=宋体, "] 4)命令替换[color=rgb(85, 85, 85)][font=宋体, "] logs=(`ls /var/log*.log`)或者logs=(/var/log/*.log)[color=rgb(85, 85, 85)][font=宋体, "] colors=(`cat color.text`) 直接从文件中读取,每一行为数组的一个元素[color=rgb(85, 85, 85)][font=宋体, "] 5)用户输入[color=rgb(85, 85, 85)][font=宋体, "] read -a array[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em][size=1em][baby@beibei tmp]$ read -a color
[size=1em]blue red black white
[size=1em][baby@beibei tmp]$ echo ${color[@]} #输出所有元素
[size=1em]blue red black white

[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]数组的访问
[color=rgb(85, 85, 85)][font=宋体, "]array[index] 例:echo ${color[2]}[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]获取所有数组
[color=rgb(85, 85, 85)][font=宋体, "] 1)${array[@]}: 每个参数是一个独立的串[color=rgb(85, 85, 85)][font=宋体, "] 2)${array[*]}: 所有参数是一个串[color=rgb(85, 85, 85)][font=宋体, "] 下面是一个脚本的代码:[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em]6
[align=right][size=1em]7
[align=right][size=1em]8
[align=right][size=1em]9
[align=right][size=1em][size=1em]#!/bin/bash
[size=1em]declare -a color
[size=1em]color=(red blue yellow black white green)
[size=1em]for i in "${color[@]}"; do
[size=1em] echo $i
[size=1em]done
[size=1em]for i in "${color[*]}"; do
[size=1em] echo $i
[size=1em]done

[color=rgb(85, 85, 85)][font=宋体, "] 以下是执行结果:
[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em]6
[align=right][size=1em]7
[align=right][size=1em]8
[align=right][size=1em][size=1em][baby@beibei bashtest]$ ./arraytest.sh
[size=1em]red
[size=1em]blue
[size=1em]yellow
[size=1em]black
[size=1em]white
[size=1em]green
[size=1em]red blue yellow black white green

[color=rgb(85, 85, 85)][font=宋体, "]如此可以看出${array[@]}与${array[*]}的区别,一个是一整个字符串,一个是每个单独的元素是一个字符串。当然如果把这里的引号去掉"${color[*]}"。for循环也可以正确输出其中的每一个元素。[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]数组的长度[color=rgb(85, 85, 85)][font=宋体, "] 1)${#ARRAY[*]}[color=rgb(85, 85, 85)][font=宋体, "] 2)${#ARRAY[@]}[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em][size=1em][baby@beibei tmp]$ array=(red yellow green blue)
[size=1em][baby@beibei tmp]$ echo ${#array[*]}
[size=1em]4

[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em][size=1em][baby@beibei tmp]$ array=([2]=green [5]=yellow [0]=blue)
[size=1em][baby@beibei tmp]$ echo ${#array[@]}
[size=1em]3 #没有定义的下标数组不会占用数组中元素的个数

[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]从数组中挑选部分元素
[color=rgb(85, 85, 85)][font=宋体, "]${ARRAY[@]:offset:number}
[color=rgb(85, 85, 85)][font=宋体, "] offset: 偏移的元素个数[color=rgb(85, 85, 85)][font=宋体, "] number: 取出的元素的个数 [color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em][size=1em][baby@beibei tmp]$ array=(red yellow green blue)
[size=1em][baby@beibei tmp]$ echo ${array[@]:1:2}
[size=1em]yellow green

[color=rgb(85, 85, 85)][font=宋体, "]取出偏移量后的所有元素[color=rgb(85, 85, 85)][font=宋体, "]${ARRAY[@]:offset}[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em][size=1em][baby@beibei tmp]$ echo ${color[@]:1}
[size=1em]yellow green blue

[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]连接两个数组[color=rgb(85, 85, 85)][font=宋体, "]array=(${array1[@]} ${array2[@]})
[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em][size=1em][baby@beibei tmp]$ number=(3 5 1 9 7)
[size=1em][baby@beibei tmp]$ color=(blue grey white red)
[size=1em][baby@beibei tmp]$ array=(${number[@]} ${color[@]})
[size=1em][baby@beibei tmp]$ echo ${array[@]}
[size=1em]3 5 1 9 7 blue grey white red

[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]数组元素的删除与替换[color=rgb(85, 85, 85)][font=宋体, "]array=(${array[@]/pattern1/string})[color=rgb(85, 85, 85)][font=宋体, "]pattern可以是一个字符串,也可以是通配符,被匹配到的字符将被替换成string,若string不写,则匹配到的元素将被删除[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]元素删除:[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em]6
[align=right][size=1em][size=1em][baby@beibei tmp]$ color=(blue1 blue2 red green)
[size=1em][baby@beibei tmp]$ echo ${color[@]}
[size=1em]blue1 blue2 red green
[size=1em][baby@beibei tmp]$ color=(${color[@]/blue*/})
[size=1em][baby@beibei tmp]$ echo ${color[@]}
[size=1em]red green

[color=rgb(85, 85, 85)][font=宋体, "]元素替换:
[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em][size=1em][baby@beibei tmp]$ color=(blue1 blue2 red green)
[size=1em][baby@beibei tmp]$ color=(${color[@]/blue*/white})
[size=1em][baby@beibei tmp]$ echo ${color[@]}
[size=1em]white white red green

[color=rgb(85, 85, 85)][font=宋体, "]当然元素的删除也可以这样:
[color=rgb(85, 85, 85)][font=宋体, "]unset ARRAY[index][color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em][size=1em][baby@beibei tmp]$ color=(blue1 blue2 red green)
[size=1em][baby@beibei tmp]$ unset color[1]
[size=1em][baby@beibei tmp]$ echo ${color[@]}
[size=1em]blue1 red green

[color=rgb(85, 85, 85)][font=宋体, "]
[color=rgb(85, 85, 85)][font=宋体, "]遍历数组[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em]6
[align=right][size=1em][size=1em]#!/bin/bash
[size=1em]color=(red green yellow white black)
[size=1em]for i in ${color[@]}; do
[size=1em] echo $i
[size=1em]done
[size=1em]exit 0

[color=#555555][font=宋体, "][font=Consolas, "][size=1em]
[tr=none ][align=right][align=right][size=1em]1
[align=right][size=1em]2
[align=right][size=1em]3
[align=right][size=1em]4
[align=right][size=1em]5
[align=right][size=1em][size=1em]color=(red green yellow white black)
[size=1em]for((i=0;i<${#color[@]};i++)); do [size=1em] echo ${color[$i]} [size=1em]done [size=1em]exit 0 [color=rgb(85, 85, 85)][font=宋体, "].......

本文标签:
本文标题:bash数组
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】