[英]How do I print the return value of a program in a loop in a batch file script?
The task is to run a program(the same program) for ten times and output for each run the exit code(the return value of the main function). 任务是运行一个程序(同一程序)十次,并在每次运行时输出退出代码(主函数的返回值)。 So I want to run a batch file(Windows), like this: 所以我想运行一个批处理文件(Windows),像这样:
FOR /l %%x IN (1,1,10) DO (
AutomatedTest.exe cip.log
ECHO %ERRORLEVEL%
)
The code above should do it if you're thinking intuitively, but it doesn't work because the code that it's running is actually: 如果您凭直觉思考,上面的代码应该可以执行此操作,但是它不起作用,因为它正在运行的代码实际上是:
(
AutomatedTest.exe cip.log
ECHO 0
)
and this piece is executed 10 times. 并执行10次。
Any ideas on how to make it work? 关于如何使其运作的任何想法? Thanks! 谢谢!
What you need is delayed variable expansion: 您需要的是延迟变量扩展:
FOR /l %%x IN (1,1,10) DO (
AutomatedTest.exe cip.log
ECHO !ERRORLEVEL!
)
To enable delayed variable expansion precede your batch with SETLOCAL ENABLEDELAYEDEXPANSION
or start command shell with CMD.EXE /V:ON
. 要启用延迟的变量扩展,请在批处理之前使用SETLOCAL ENABLEDELAYEDEXPANSION
或使用CMD.EXE /V:ON
启动命令外壳。
Another approach is using subroutines: 另一种方法是使用子例程:
FOR /l %%x IN (1,1,10) DO CALL :Test
GOTO :EOF
:Test
AutomatedTest.exe cip.log
ECHO %ERRORLEVEL%
GOTO :EOF
Yet another approach is to use IF ERRORLEVEL
. 另一种方法是使用IF ERRORLEVEL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.