[英]“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.