什么是数组呢?
在编程语言中,数组是具有相同数据类型变量的集合,在内存中存储在一段连续的空间里。在 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]
#!/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
[root@server array]
32181
30658
26576
16071
6521
32181 30658 26576 16071 6521
|
五、数组的长度
${#a[*]},${#a[@]} 都可以表示数组的长度,在遍历数组时会使用到。
1
2
3
4
5
|
[root@server array]
[root@server array]
6
[root@server array]
6
|
6、从数组中挑选某元素:
${ARRAY[@]:offset:number}
offset: 偏移的元素个数
number: 取出的元素的个数
1
2
3
|
[root@server array]
[root@server array]
2 more
|
${ARRAY[@]:offset}:取出偏移量后的所有元素
1
2
|
[root@server array]
more less tail head
|
${ARRAY[@]}: 取出所有元素
1
2
|
[root@server array]
1 2 more less tail head
|
七、向数组追加元素和从数组中删除元素
这里一般针对的是非关联数组,也就是使用数字作为下标的数组
追加元素:
1
2
3
4
5
|
[root@server array]
[root@server array]
[root@server array]
1 2 more less tail head 33
|
删除数组中的元素,使用unset ARRAY[index]
1
2
3
|
[root@server array]
[root@server array]
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 i in ` seq 0 ${
if [ $[$i%2] - eq 0 ]; then
index=${
mylogs[$index]=${logs[$i]}
fi
done
echo ${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]
/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 i in {0..9}; do
arr[$i]=$RANDOM
done
echo "${arr[@]}"
let -i tmp
for i in ` seq 0 $[${
for j in ` seq $[$i+1] $[${
if [ ${arr[$j]} -lt ${arr[$i]} ]; then
tmp=${arr[$j]}
arr[$j]=${arr[$i]}
arr[$i]=$tmp
fi
done
done
echo "${arr[@]}"
|
执行结果:
1
2
3
|
[root@server array]
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,如需转载请自行联系原作者