shell--数组的定义/访问/赋值/遍历


 1 #!/bin/bash
 2 # 数组
 3 
 4 # 数组的定义
 5 a=(0 1 2 3)
 6 # 数组元素的访问
 7 echo "a[0]:${a[0]}"
 8 # 数组的长度
 9 echo "length:${#a[*]}"
10 # 所有元素
11 echo "all element:${a[*]}"
12 # 删除某个元素
13 unset a[1]
14 echo "after unset:${a[*]}"
15 
16 echo "用for in 遍历"
17 for every in ${a[*]}
18 do
19     echo "every:$every"
20 done
21 
22 
23 echo "用for(())遍历"
24 for ((i=0;i<=5;i++))
25 do
26     array[$i]=0
27 done
28 
29 for ((i=0;i<${#array[*]};i++))
30 do
31     echo ${array[$i]}
32 done

 

 

输出:

a[0]:0
length:4
all element:0 1 2 3
after unset:0 2 3
用for in 遍历
every:0
every:2
every:3
用for(())遍历
0
0
0
0
0
0

posted @ 2014-05-25 22:23  helloweworld  阅读(1072)  评论(0编辑  收藏  举报