问题描述
我试图链使用 EXIT / B X
命令返回成功或失败,并与功放一系列.bat文件;&安培;和||下一个蝙蝠的条件运行(如 a.bat和放大器;&安培; b.bat
)。
I'm trying to chain a series of .bat files using the EXIT /B X
command to return success or failure and && and || for conditional running of the next .bat (e.g. a.bat && b.bat
).
不管我是否叫 EXIT / B 0
或其他任何结束a.bat, a.bat和放大器;&安培; b.bat
将随后致电b.bat。我的理解是, EXIT / B 0
应该设置 ERRORLEVEL = 0
,这是成功的,所以和放大器;&安培;应该继续。在对位这是调用 EXIT / B 1
应该设置 ERRORLEVEL = 1
这是失败的,所以和放大器; &安培;应该停止。缺少什么我在这里?
Regardless of whether I call EXIT /B 0
or anything else to end a.bat, a.bat && b.bat
will call b.bat afterward. My understanding is that EXIT /B 0
should set ERRORLEVEL=0
, which is success, so the && should continue. The counterpoint to this is that calling EXIT /B 1
should set ERRORLEVEL=1
which is failure, so the && should stop. What am I missing here?
轻视例如:
有关非批处理命令,作为预期的
For non-batch commands, acting as expected
C:\>echo test|findstr test>NUL && echo yes
yes
C:\>echo test|findstr test>NUL || echo yes
C:\>echo test|findstr nope>NUL && echo yes
C:\>echo test|findstr nope>NUL || echo yes
yes
C:\>
使用EXIT / B总是能看到a.bat为成功
Using EXIT /B always sees a.bat as successful
C:\>echo @EXIT /B 0 > a.bat
C:\>a.bat && echo yes
yes
C:\>a.bat || echo yes
C:\>echo @EXIT /B 1 > a.bat
C:\>a.bat && echo yes
yes
C:\>a.bat || echo yes
C:\>
如何从a.bat退出以使 a.bat和放大器;&安培; b.bat
和 a.bat || b.bat
像预期的那样?
所有的命令都输入cmd.exe在Windows XP SP3上运行。
All commands are run in cmd.exe on Windows XP SP3.
推荐答案
如果你问我,退出codeS在批处理文件被破坏了这个确切原因,但你可以使用一个哈克解决方法。当您的批处理文件的最后一行,使用方法:
If you ask me, exit codes in batch files are broken for this exact reason, but there is a hacky workaround you can use. As the last line of your batch file, use:
@%COMSPEC% /C exit 1 >nul
由于这是开始,你得到一个真正的进程退出code和放大器的实际过程;&安培;和||会工作。
Since this is an actual process that is started you get a real process exit code and && and || will work.
这篇关于退出批次`EXIT / B X`其中X> = 1就如同命令,使用和放大器时,成功完成;&安培;或||一批通话之间运营的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!