批处理文件可以捕获它正在调用的命令的退出代码吗?
基本上,假设我有一个调用 myapp1.exe 的批处理文件,并且 myapp1.exe 以退出代码 1 退出。批处理文件能否捕获此信息并强制批处理文件以相同的退出代码退出或执行其他一些逻辑?
Basically, let's say that I have a batch file that calls myapp1.exe and myapp1.exe exits with Exit Code 1. Can the batch file capture this information and either force the batch file to exit with that same exit code or perform some other logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将是显式变体。
would be the explicit variant.
接受的答案是正确的,但如果您使用
call
调用另一个批处理脚本,并且第二个批处理脚本使用SetLocal
,您可能需要使用解析技巧来完成这个。如果您遇到这种情况,请在exit b
之前添加以下代码:现在,
myvariable
的值可供调用上下文使用,您可以在其他脚本。参考资料:
https://stackoverflow.com/a/16167938/89590
http://www.borngeek.com/2008/05 /22/退出批处理文件上下文/
The accepted answer is correct, but if you are using
call
to call another batch script, and that second batch script is usingSetLocal
, you may need to use a parsing trick to accomplish this. If you are running into this, add the following code before yourexit b
:Now the value of
myvariable
is made available to the calling context and you can see the value in the other script.References:
https://stackoverflow.com/a/16167938/89590
http://www.borngeek.com/2008/05/22/exiting-batch-file-contexts/
您可以尝试使用
errorlevel
。更多信息请参见此处。You could try using
errorlevel
s. Some more info here.%ERRORLEVEL% 存储最后执行命令的返回值
%ERRORLEVEL% stores the return value of last executed command