十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

【转】linux shell 数组建立及使用技巧

上一篇 / 下一篇  2011-06-14 13:41:13 / 个人分类:liunx相关

linux shell在编程方面比windows 批处理强大太多,无论是在循环、运算。已经数据类型方面都是不能比较的。 下面是个人在使用时候,对它在数组方面一些操作进行的总结。

8a%G!KU5Gd0

1.数组定义51Testing软件测试网ZG3a*uX6Y}4}.X-F

[chengmo@centos5 ~]$ a=(1 2 3 4 5)
` Q3anEV&?&v0[chengmo@centos5 ~]$ echo $a51Testing软件测试网2_ yjn/@.f
151Testing软件测试网Xtt9bea9M)X6w

一对括号表示是数组,数组元素用“空格”符号分割开。

9{;~-|c;_2oW/Mh0

2.数组读取与赋值51Testing软件测试网+JLo/h0\B4n'q

  • 得到长度

[chengmo@centos5 ~]$ echo ${#a[@]}
bLd x+g G0551Testing软件测试网b ^ gTE0{2rW

用${#数组名[@或*]} 可以得到数组长度51Testing软件测试网]OUK7? V

  • 读取

[chengmo@centos5 ~]$ echo ${a[2]} 
]u'm@)B IYmeG03

4X^&DD a;Py0

[chengmo@centos5 ~]$ echo ${a[*]} 
-y g6k[x} T"z01 2 3 4 5   

K"w/iH1A?t"p~0

用${数组名[下标]} 下标是从0开始  下标是:*或者@ 得到整个数组内容51Testing软件测试网k_bUqF?f

  • 赋值:

[chengmo@centos5 ~]$ a[1]=100

tH9S^N;h+b4?0

[chengmo@centos5 ~]$ echo ${a[*]} 51Testing软件测试网'Uw5{y'Ekr
1 100 3 4 5

~"e3}C"l oR%d0

[chengmo@centos5 ~]$ a[5]=100     51Testing软件测试网aADe~0\ z;nd~r\
[chengmo@centos5 ~]$ echo ${a[*]}

,Og3V7`%zaoI0

1 100 3 4 5 100

V&k!r[+b:g;d Ist\0

直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素

OR4vy!BB;w F0
  • 删除:

[chengmo@centos5 ~]$ a=(1 2 3 4 5)51Testing软件测试网$h Y5eT9]4A@
[chengmo@centos5 ~]$ unset a51Testing软件测试网Db8mq;hk dh\
[chengmo@centos5 ~]$ echo ${a[*]}

)Ye2l:ri\7Uz|2x b0

[chengmo@centos5 ~]$ a=(1 2 3 4 5)51Testing软件测试网&E-w"Et}@ zd
[chengmo@centos5 ~]$ unset a[1]   
{G+y U.{ U7Tgad0[chengmo@centos5 ~]$ echo ${a[*]} 51Testing软件测试网"qf"F"rC Ji
1 3 4 5
Zh+vh%D1y4l8Tx0[chengmo@centos5 ~]$ echo ${#a[*]}51Testing软件测试网 Hs0ztcX}Mz7x
4

&e1ud(fZ T`'D+n*ct0

直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据。51Testing软件测试网3JLn'C;]S s l

3.特殊使用51Testing软件测试网$Q(M4n"k/w sj&J

  • 分片:

[chengmo@centos5 ~]$ a=(1 2 3 4 5)
{A&s\U&`'G9K0[chengmo@centos5 ~]$ echo ${a[@]:0:3}
pQpq%h*~q$k01 2 3
2p6[pQ}JB%yB Q0[chengmo@centos5 ~]$ echo ${a[@]:1:4}
r%^0f~RI Uf3R02 3 4 551Testing软件测试网D,Wp$C:Y3[4tA;^

[chengmo@centos5 ~]$ c=(${a[@]:1:4})51Testing软件测试网l sL _&m*}S
[chengmo@centos5 ~]$ echo ${#c[@]}51Testing软件测试网W:^s!I;e.Y
451Testing软件测试网(n$O4Kb&M[;C{
[chengmo@centos5 ~]$ echo ${c[*]} 51Testing软件测试网 V t9A"_A`"]2fp
2 3 4 5

9K1C@eck*K9] v5X.g0

直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,上面例子:c 就是一个新数据。51Testing软件测试网)l(a4ml#U_a}t*T

  • 替换:

[chengmo@centos5 ~]$ a=(1 2 3 4 5)    51Testing软件测试网 Fx4tm,Qd%I
[chengmo@centos5 ~]$ echo ${a[@]/3/100}51Testing软件测试网.g1XS.]5c1Q"p
1 2 100 4 551Testing软件测试网,t1R` _r gy
[chengmo@centos5 ~]$ echo ${a[@]}
\J*N-UUfW01 2 3 4 5
-or%cld0[chengmo@centos5 ~]$ a=(${a[@]/3/100}) 
UO9Ka)m m(r'{0[chengmo@centos5 ~]$ echo ${a[@]}     
\:R'SA U.R i3{ K01 2 100 4 5

kG%K%Cc5k0

调用方法是:${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容,如果需要修改,可以看上面例子,重新定义数据。

,yO%G-ou#y6pY0

 

0A(_.^(i4L'k0sC0

从上面讲到的,大家可以发现linux shell 的数组已经很强大了,常见的操作已经绰绰有余了。

6CWA|h)L6e O0

转载http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html

[W(Z9U D0

shell 按行读取并保存成数组

ho}`] L(`rQ0

从ip.txt里读取IP.然后把IP地址赋值到一个数组里.51Testing软件测试网.m1p4E,a2\7s9Y0L
IP文件如下:
3U d{;P PVb$}:k+O0Address:  220.181.26.163
@]R2F hfE V0Address:  220.181.26.17451Testing软件测试网O|H#u.dx q
Address:  220.181.26.17551Testing软件测试网0N!A#K jY Xm
Address:  220.181.26.176
J|1["g~e:f"gR0Address:  220.181.19.22851Testing软件测试网B2w-`SQF
Address:  220.181.19.22951Testing软件测试网"{9^Iun4Ka
Address:  220.181.26.161
@ }uo$UF+Z4f0Address:  220.181.26.162

p8eA8\;S&K-?0

方法一:
%E9`w+w Cr0for x in ` awk '{print $2}' ip.txt `
0AnZy:@z2F@6g0{51Testing软件测试网halPWW\i+d%j
echo $x51Testing软件测试网5\R#w9^*m){vG\7\
}
bn^-yjy5a7]0方法二:
Dm&M/NW0ARRAY=($(awk '{print $2}' ip.txt))51Testing软件测试网-_n Z~6}eh#rp q
方法三:
9R*@2J TUB/C+kt0n=0;while read a b;do array[$n]=$b;((n++));done<ip.txt

p H\h)NjU#{Yg0

方法四:51Testing软件测试网C P?MZiU
n=151Testing软件测试网7uAn(~*h c:ANB
while ((n<=$(cat ip.txt|wc -l)))
;Qm6A`vg0do
$zg4Wk,C.i0    ipaddr[$n]=$(cat ip.txt|sed -n "${n}p"|awk '{print $2}')
?@:s8k(Zh*l0    ((n+=1))51Testing软件测试网2u|'}8O0[4{ ?
done51Testing软件测试网o.n0yZ-gd }}
n=`expr $n - 1`51Testing软件测试网dWZ5\1Jc;U



TAG: shell Shell

 

评分:0

我来说两句

Open Toolbar