bash数组

编程

有2种数组:

  • 索引数组
  • 关联数组(java里的Hashmap),用key关联value

声明数组:

#声明索引数组

declare -a ary

#声明关联数组

declare -a ary

引用数组:${ary[idx]}

必须加大括号

直接访问数组名,就是访问第一个元素

[root@localhost ~]# ani[0]=dog

[root@localhost ~]# ani[1]=cat

[root@localhost ~]# echo ani

ani

[root@localhost ~]# echo ani[0]

ani[0]

[root@localhost ~]# echo $ani[0]

dog[0]

[root@localhost ~]# echo ${ani[0]}

dog

[root@localhost ~]# echo ${ani[1]}

cat

给数组赋值

  • 单个赋值:ary[idx]=val

  • 多个赋值:ary=(val1 val2...)

    [root@localhost ~]# ary=(1 2 3 4 )

    [root@localhost ~]# echo $ary

    1

    [root@localhost ~]# echo ${ary[0]}

    1

    [root@localhost ~]# echo ${ary[2]}

    3

    [root@localhost ~]# echo ${ary[1]}

    2

    [root@localhost ~]# echo ${ary[3]}

    4

    [root@localhost ~]# echo ${ary[4]}

    [root@localhost ~]#

  • 跳跃赋值:ary=([0]="aa" [3]="dd")

    [root@localhost ~]# ary=([0]="aa" [3]="dd")

    [root@localhost ~]# echo ${ary[0]}

    aa

    [root@localhost ~]# echo ${ary[1]}

    [root@localhost ~]# echo ${ary[3]}

    dd

read读取数组

就选项-a就是读取数组

# read -a li

a b 1 d a2

[root@localhost ~]# echo $li

a

[root@localhost ~]# echo ${li[0]}

a

[root@localhost ~]# echo ${li[2]}

1

[root@localhost ~]# echo ${li[4]}

a2

取得数组元素的个数

  • ${#ary[*]}
  • ${#ary[@]}

取得数组中某个元素的长度

${#ary[indx/名字]}

得到数组的所以元素

  • ${ary[*]}
  • ${ary[@]}

关联数组

关联数组使用前最好先声明,使用-A选项声明。

[root@localhost scripts]# declare -A map

[root@localhost scripts]# map=([aa]=df12 [dd]=132432 [2]=12d)

[root@localhost scripts]# echo ${map[*]}

132432 df12 12d

[root@localhost scripts]# echo ${map[aa]}

df12

[root@localhost scripts]# echo ${map[2]}

12d

[root@localhost ~]# declare -A l3

[root@localhost ~]# l3[a]=aa

[root@localhost ~]# l3[b]=bb

[root@localhost ~]# echo ${l3[a]}

aa

[root@localhost ~]# echo ${l3[b]}

bb

[root@localhost ~]# echo ${#l3[*]}

2

[root@localhost ~]# echo ${#l3[@]}

2

[root@localhost ~]# echo ${#l3[a]}

2

[root@localhost ~]# echo ${#l3[b]}

2

[root@localhost ~]# l3[c]=1dsfs

[root@localhost ~]# echo ${#l3[c]}

5

[root@localhost ~]# echo ${l3[*]}

aa bb 1dsfs

[root@localhost ~]# echo ${l3[@]}

aa bb 1dsfs

数组的切片

${ary[@]:offset:length}

  • offset:从第几个开始切
  • length:切多长

[root@localhost scripts]# l1=(1 2 3 4 5 6)

[root@localhost scripts]# echo ${l1[@]:2:2}

3 4

删除数组里某个元素

# l1=(1 2 3 4 5 6)

[root@localhost scripts]# echo ${l1[@]:2:2}

3 4

[root@localhost scripts]# unset l1[1]

[root@localhost scripts]# echo ${l1[@]}

1 3 4 5 6

练习

1,生成10个随机整数,求最大值

$RANDOM是内置变量,能生成随机整数。

#!/bin/bash

#

declare -a rand

declare -i max=0

for i in {0..9}; do

rand[$i]=$RANDOM

echo ${rand[$i]}

if [ $max -lt ${rand[$i]} ]; then

max=${rand[$i]}

fi

done

echo "max is $max"

2,统计/var/log/*.log文件的行数之和。

关键点:files=(/var/log/*.log)。括号里是globbing风格,会自动展开。

#!/bin/bash

#

declare -a files

declare -i lines

files=(/var/log/*.log)

for ((i=0;i<${#files[*]};i++)); do

let lines+=`wc -l ${files[$i]} | cut -d" " -f1`

done

echo $lines

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

以上是 bash数组 的全部内容, 来源链接: utcz.com/z/514070.html

回到顶部