且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux Shell编程之四数组

更新时间:2022-09-17 15:24:08

什么是数组呢?

    在编程语言中,数组是具有相同数据类型变量的集合,在内存中存储在一段连续的空间里。在 Linux shell 中数组也是这么定义,但用法和使用格式具有自己的特点。

    下面从不同角度来解析数组。


一、数组的表示

    索引表示,一般使用数字做下标,从 0 开始:

例如:a[0],a[1],a[2]

    关联数组,数组的下标可以使用任意字符(符合变量命名的规则):

例如:a[/bin/bash],a[192.168.0.1]

    在 shell 中只支持一维数组,支持稀疏格式。


二、数组的定义

    declaer -a sum

    read -p "please input a array:"-a sum

    declare -A sum  # 定义关联数组,如果使用关联数组必须先定义


三、数组赋值

    一次只对一个赋值

a[0]=1,a[2]=$RANDOM

    一次对全部元素赋值,例如:

a=(red yellow black tigger mouse)

    按索引进行赋值

a=([1]=less [9]=more [10]=cat [8]=tail) # 稀疏格式存储

四、数组访问

    用索引访问,例如:

a[0] a[more]

    引用数组的值

${a[0]},${a[more]}

    全部引用:

${a[@]},${a[*]} 一般情况下使用 ${a[@]} 这种形式,具体使用格式区别如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@server array]# cat test\@.sh 
#!/bin/bash
for ((i=0;i < 5;i++));do
    a[$i]=$RANDOM
done
for loop in "${a[@]}";do
    echo $loop
done
for loop in "${a[*]}";do
    echo $loop
done
 
# ${a[@]} 这种形式列出所有的参数,每个参数是一个独立的字符串
# ${a[*]} 也是列出所有的参数,单数所有的参数是一个字符串
# 执行结果如下:
[root@server array]# ./test\@.sh 
32181
30658
26576
16071
6521
32181 30658 26576 16071 6521


五、数组的长度

    ${#a[*]},${#a[@]} 都可以表示数组的长度,在遍历数组时会使用到。

1
2
3
4
5
[root@server array]# a=(1 2 more less tail head)
[root@server array]# echo ${#a[@]}
6
[root@server array]# echo ${#a[*]}
6


6、从数组中挑选某元素:

    ${ARRAY[@]:offset:number}

        offset: 偏移的元素个数

        number: 取出的元素的个数

1
2
3
[root@server array]# a=(1 2 more less tail head)
[root@server array]# echo ${a[@]:1:2}
more

    ${ARRAY[@]:offset}:取出偏移量后的所有元素

1
2
[root@server array]# echo ${a[@]:2}
more less tail head

    ${ARRAY[@]}: 取出所有元素

1
2
[root@server array]# echo ${a[@]}
1 2 more less tail head


七、向数组追加元素和从数组中删除元素

    这里一般针对的是非关联数组,也就是使用数字作为下标的数组

    追加元素:

1
2
3
4
5
# 新追加的元素的下标是数组的长度
[root@server array]# a=(1 2 more less tail head)
[root@server array]# a[${#a[@]}]=33
[root@server array]# echo ${a[@]}
1 2 more less tail head 33

    删除数组中的元素,使用unset ARRAY[index]

1
2
3
[root@server array]# unset a[2]
[root@server array]# echo ${a[@]}
1 2 less tail head 33


八、数组的一些应用

1、复制一个数组中下标为偶数的元素至一个新数组中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
declare -a mylogs
logs=(/var/log/*.log)
 
echo ${logs[@]}
 
for in `seq 0 ${#logs[@]}`; do
    if [ $[$i%2] -eq 0 ];then
        index=${#mylogs[@]}
        mylogs[$index]=${logs[$i]}
    fi
done
 
echo ${mylogs[@]}
 
# 这里最关键的是这一句 index=${#mylogs[@]}
# 实现了新数组的下标的设定


2、统计 /etc/passwd 中使用各个shell的次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
 
declare -A sum
     
while read line ;do
    index=`echo $line | cut -d: -f7`
    let sum[$index]++
done /etc/passwd
     
index_set=`cut -d: -f7 /etc/passwd sort uniq`
 
for loop in $index_set;do
    echo "$loop in /etc/passwd total have ${sum[$loop]} times."
done
 
# 这里主要是考察关联数组的应用。真正在实际中这种用法较多。

执行结果如下:

1
2
3
4
5
6
7
[root@server array]# ./sumbash.sh 
/bin/bash in /etc/passwd total have 56 times.
/bin/sync in /etc/passwd total have 1 times.
/bin/tcsh in /etc/passwd total have 1 times.
/sbin/halt in /etc/passwd total have 1 times.
/sbin/nologin in /etc/passwd total have 33 times.
/sbin/shutdown in /etc/passwd total have 1 times.


2、将 10 个随机数按升序排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
 
for in {0..9};do
    arr[$i]=$RANDOM
done
 
echo "${arr[@]}"
 
let -i tmp
 
for in `seq 0 $[${#arr[@]}-1]`;do
    for in `seq $[$i+1] $[${#arr[@]}-1]` ;do
        if [  ${arr[$j]} -lt ${arr[$i]} ];then
            tmp=${arr[$j]}
            arr[$j]=${arr[$i]}
            arr[$i]=$tmp
        fi 
    done
done
 
echo "${arr[@]}"
 
# 这个示例只在说明数组的使用形式,shell 中的数组一般不进行这样的工作。

执行结果:

1
2
3
[root@server array]# ./sort.sh 
749 18853 896 20690 4794 395 23231 24049 4906 21630
395 749 896 4794 4906 18853 20690 21630 23231 24049

    至此,数组的基本使用就介绍完毕。










本文转自 羊木狼 51CTO博客,原文链接:http://blog.51cto.com/guoting/1534111,如需转载请自行联系原作者