[英]“EXIT” in batch file cannot overwrite return code of previous commands
[英]How to get return/exit code of a batch file started with START command?
我正在尝试使用start /high
运行批处理文件,但仍然获得返回/退出代码,即%ERRORLEVEL%
。 问题似乎是命令START不返回批处理文件返回的退出代码。
我们有一个简单的批处理文件,用于测试名为BatFileThatReturnsOne.bat
测试。
BatFileThatReturnsOne.bat
的内容是
EXIT /B 1
我们这样称呼:
start /high /wait BatFileThatReturnsOne.bat
但无论批处理文件返回什么, start
的执行从不具有除0(零)以外的任何值的%ERRORLEVEL%
。
这实际上是由CloudFormation中的cfn-init调用的,但这可能不相关,因为我们可以从命令行窗口重现它。
实际的电话是:
cmd.exe /C start /high /wait BatFileThatReturnsOne.bat
如何start
将%ERRORLEVEL%
设置为0(零)以外的值?
直接来自cmd
窗口或您可以使用的批处理文件
start /high /wait cmd /c BatFileThatReturnsOne.bat
但是如果您需要启动cmd
实例来执行启动批处理文件的启动命令,那么您可以使用
cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"
只需通过EXIT 1
更改EXIT /B 1
EXIT 1
。
如本答案中给出的关于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.