Windows shell嵌套for循环

时间:2016-02-26 10:54:40

标签: shell for-loop windows-shell

我想用两个增加的输入参数多次运行程序。为此,我必须编写一个包含两个嵌套for循环的小shell脚本。出于某种原因,我不理解脚本在打印所有值之前停止执行。

for /l %%r in (1 2 3 4 5 6 7 8 9 10) do (
    for /l %%c in (1 2 3 4 5 6 7 8 9 10) do (
        rem Run my program here!
        echo %%r %%c 
    )
)

输出

1 1
1 3
3 1
3 3

我真的迷路了,因为我不是Windows shell编程方面的专家。

1 个答案:

答案 0 :(得分:1)

您已添加/ l标志,该标志具有以下属性:

for /l %%X in (start, step, end) do command

所以在你的情况下:

Start: 1
Step : 2
End  : 3

这就是为什么你得到的:

(1, 1), (1,3), (3,1), (3,3)

如果删除/ l,则应按预期获得结果。

echo off
for  %%r in (1 2 3 4 5 6 7 8 9 10) do (
    for  %%c in (1 2 3 4 5 6 7 8 9 10) do (
        echo %%r %%c 
    )
)

返回(1,1)到(10,10)。

您可以找到更多信息here

相关问题