bash中的数组

bash脚本test.sh

1
2
3
4
5
6
7
8
9
10
11
index=10
for i in `seq 0 9`
do
arr[${i}]=${index}
index=$((${index}+1))
done

for i in `seq 0 9`
do
echo ${arr[${i}]}
done
1
2
3
4
5
6
7
8
9
10
11
sh test.sh
10
11
12
13
14
15
16
17
18
19

dash中的数组替代方法

dash脚本test.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
vector_get()
{
local name="$1"
local i="$2"
eval echo '$'{${name}${i}}
}
vector_set()
{
local name="$1"
local i="$2"
local v="$3"
eval ${name}${i}=${v}
}

n=10
for i in `seq 0 9`
do
vector_set arr ${i} ${n}
n=$((${n}+1))
done

for i in `seq 0 9`
do
v=`vector_get arr ${i}`
echo $v
done

1
2
3
4
5
6
7
8
9
10
11
sh test.sh
10
11
12
13
14
15
16
17
18
19