我刚刚开始学习如何使用数组,所以如果答案有点明显,我很抱歉。我做了一个简单的程序,我以为它会正常计数。这个程序的输出不仅一开始是倒着的,而且非常奇怪。程序是:
#!/bin/bash
num=1
declare -A A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
但它的输出在 8 之后开始做一些非常奇怪的事情,如下所示:
1
e is 1
num is 2
2
1
e is 1
num is 3
3
2
1
e is 1
num is 4
4
3
2
1
e is 1
num is 5
5
4
3
2
1
e is 1
num is 6
6
5
4
3
2
1
e is 1
num is 7
7
6
5
4
3
2
1
e is 1
num is 8
8
7
6
5
4
3
2
1
e is 1
num is 9
8
7
6
5
4
3
2
1
9
e is 9
num is 10
8
7
6
5
4
3
2
1
9
10
e is 10
num is 11
8
7
6
5
4
3
2
1
11
9
10
e is 10
num is 12
8
7
6
5
4
3
2
1
11
12
9
10
e is 10
num is 13
8
7
6
5
4
3
2
1
11
12
9
10
13
e is 13
num is 14
8
7
6
5
4
3
2
1
11
12
9
10
13
14
e is 14
num is 15
8
7
6
5
4
3
2
1
11
12
9
10
15
13
14
e is 14
num is 16
8
7
6
5
4
3
2
1
11
12
9
10
15
16
13
14
e is 14
num is 17
8
7
6
5
4
3
2
1
17
11
12
9
10
15
16
13
14
e is 14
num is 18
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
e is 14
num is 19
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
19
e is 19
num is 20
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
20
19
e is 19
num is 21
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
21
20
19
e is 19
num is 22
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
22
21
20
19
e is 19
num is 23
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
23
22
21
20
19
e is 19
num is 24
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
24
23
22
21
20
19
e is 19
num is 25
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
25
24
23
22
21
20
19
e is 19
num is 26
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
e is 19
num is 27
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
27
e is 27
num is 28
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
e is 27
num is 29
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
e is 29
num is 30
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
e is 30
num is 31
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
e is 31
num is 32
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
32
e is 32
num is 33
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
29
30
31
32
e is 32
num is 34
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
29
30
31
32
e is 32
num is 35
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
29
30
31
32
e is 32
num is 36
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
e is 32
num is 37
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
e is 37
num is 38
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 39
39
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 40
^C
有人能解释一下吗?
答案1
您已声明关联数组,因此输出无序。更改declare -A A1
为declare -a A1
声明索引数组。
如下图所示:
#!/bin/bash
num=1
declare -a A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
此外,您的数组索引不是从零开始的。第一个元素是 A1[2]。要修复它,请将循环内的第一行更改为A1[$(($num - 1))]=$num