我正在尝试运行使用列表作为循环计数器的嵌套 for 循环。问题是,一旦“增量”循环达到 100,它不会重置为 0。“边缘”也有同样的问题。
我尝试过这个,但它似乎不适用于我的循环。 http://tldp.org/LDP/abs/html/nestedloops.html
这里有什么想法吗?这是我的代码:
#!/bin/sh
threads="1 2 4 8 16 32 64 96 128 160 192 224 256"
delta="0 10 20 30 40 50 60 70 80 90 100"
edges="16 8192"
nodes="16384"
for threads in $threads
do
for delta in $delta
do
for edges in $edges
do
for nodes in $nodes
do
printf "\n"
echo $threads
echo $delta
echo $edges
echo $nodes
done
done
done
done
预期输出:
1 0 16 16384
1 0 8192 16384
1 10 16 16384
1 10 8192 16384
1 20 16 16384
1 20 8192 16384
最佳答案
当使用这样的 for
循环时,请确保为循环变量指定与要迭代的变量不同的名称。
对 $threads 中的线程使用 会使区分循环变量 (
threads
) 和要循环的对象 ($threads
)。
当您稍后调用 echo $threads
时,bash 不知道您指的是第一个线程。
在这种情况下,您可以将循环声明更改为 for n in nodes
或 for t inthreads
,然后 echo
out最内层循环内的 $n
和 $t
以获得所需的输出。
关于bash - 嵌套 For 循环计数器重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225604/