[英]How to get return/exit code of a batch file started with START command?
I am trying to run a batch file with start /high
and still get the return/exit code, ie %ERRORLEVEL%
. 我正在尝试使用
start /high
运行批处理文件,但仍然获得返回/退出代码,即%ERRORLEVEL%
。 The problem seems to be that command START does not return the exit code that the batch file returns. 问题似乎是命令START不返回批处理文件返回的退出代码。
We have a simple batch file for testing named BatFileThatReturnsOne.bat
. 我们有一个简单的批处理文件,用于测试名为
BatFileThatReturnsOne.bat
测试。
The contents of BatFileThatReturnsOne.bat
are BatFileThatReturnsOne.bat
的内容是
EXIT /B 1
We are calling this as such: 我们这样称呼:
start /high /wait BatFileThatReturnsOne.bat
But no matter what the batch file returns, the execution of start
never has a %ERRORLEVEL%
of anything other than 0 (zero). 但无论批处理文件返回什么,
start
的执行从不具有除0(零)以外的任何值的%ERRORLEVEL%
。
This is all actually called by cfn-init in CloudFormation, but that is probably not relevant because we can reproduce it from a command line window. 这实际上是由CloudFormation中的cfn-init调用的,但这可能不相关,因为我们可以从命令行窗口重现它。
The actual call is: 实际的电话是:
cmd.exe /C start /high /wait BatFileThatReturnsOne.bat
How do I get start
to set the %ERRORLEVEL%
to something other than 0 (zero)? 如何
start
将%ERRORLEVEL%
设置为0(零)以外的值?
directly from a cmd
window or a batch file you can use 直接来自
cmd
窗口或您可以使用的批处理文件
start /high /wait cmd /c BatFileThatReturnsOne.bat
but if you need to start the cmd
instance to execute the start command that launchs the batch file then you can use 但是如果您需要启动
cmd
实例来执行启动批处理文件的启动命令,那么您可以使用
cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"
Just change EXIT /B 1
by EXIT 1
. 只需通过
EXIT 1
更改EXIT /B 1
EXIT 1
。
As explained in the Table 4 given in this answer about START /WAIT bat
command: 如本答案中给出的关于
START /WAIT bat
命令的表4中所述:
When the started Batch file ends, set ERRORLEVEL = value from 'EXIT number' commmand.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.