windows 使用“EXIT /BX”退出批处理,其中 X>=1 就像使用 && 或 || 时命令成功完成一样 批处理调用之间的运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4632891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:56:15  来源:igfitidea点击:

Exiting batch with `EXIT /B X` where X>=1 acts as if command completed successfully when using && or || operators between batch calls

windowsbatch-filecmdexiterrorlevel

提问by Jordan Evens

I'm trying to chain a series of .bat files using the EXIT /B Xcommand to return success or failure and &&and ||for conditional running of the next .bat (e.g. a.bat && b.bat).

我试图链的一系列使用.bat文件EXIT /B X命令返回成功或失败,&&||为下一个蝙蝠(例如条件运行a.bat && b.bat)。

Regardless of whether I call EXIT /B 0or anything else to end a.bat, a.bat && b.batwill call b.bat afterward. My understanding is that EXIT /B 0should set ERRORLEVEL=0, which is success, so the &&should continue. The counterpoint to this is that calling EXIT /B 1should set ERRORLEVEL=1which is failure, so the &&should stop. What am I missing here?

无论我是调用EXIT /B 0还是其他任何方式来结束 a.bat,a.bat && b.bat之后都会调用 b.bat。我的理解是EXIT /B 0应该设置ERRORLEVEL=0,这是成功,所以&&应该继续。与此相反的是,调用EXIT /B 1应该设置ERRORLEVEL=1哪个失败,所以&&应该停止。我在这里错过了什么?

Trivialized example:

平凡的例子:

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

Using EXIT /Balways sees a.bat as successful:

使用EXIT /B总是看到 a.bat 成功:

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

How can I exit from a.bat so that a.bat && b.batand a.bat || b.batbehave as expected?

如何从a.bat退出,这样a.bat && b.bata.bat || b.bat像预期的那样?

All commands are run in cmd.exe on Windows XP SP3.

所有命令都在 Windows XP SP3 上的 cmd.exe 中运行。

采纳答案by Anders

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

Since this is an actual process that is started you get a real process exit code and && and || will work.

由于这是一个实际启动的进程,您会得到一个真正的进程退出代码和 && 和 || 将工作。

回答by marapet

It works as it should when using callto execute batch scripts containing an exit statement:

当使用call执行包含 exit 语句的批处理脚本时,它可以正常工作:

C:\>echo @EXIT /B 1 > a.bat

C:\>call a.bat && echo yes

C:\>call a.bat || echo yes
yes

By the way, it says wronglyon Microsoft docs:

顺便说一句,它在Microsoft 文档说错了:

Call has no effect at the command prompt when it is used outside of a script or batch file.

在脚本或批处理文件之外使用时,调用在命令提示符下不起作用。

回答by user2989311

If you use start /waityou can also use this in a very simple Windows application (written in C#) called by DOS batch files like so:

如果您使用,start /wait您还可以在一个非常简单的 Windows 应用程序(用 C# 编写)中使用它,该应用程序由 DOS 批处理文件调用,如下所示:

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Environment.ExitCode = Convert.ToInt32(args[0]);
    }
}

Then the application can be called by your DOS batch file and evaluate the result. i.e.

然后,您的 DOS 批处理文件可以调用该应用程序并评估结果。IE

c:> start /wait SetRC 1
c:> if "%errorlevel%"=="1" goto abort

NOTE: the /waitis not necessary in a batch file.

注意:/wait在批处理文件中不需要。

You could pass in the return code you want as an argument to your program.cs and get it out this way guaranteed.

您可以将您想要的返回代码作为参数传递给您的 program.cs 并以这种方式保证将其取出。

回答by TheDude

I think you are getting Errorlevel=0with because you are indeed executing a.bat (regardless of the return code).

我认为您正在接受Errorlevel=0,因为您确实在执行 a.bat (无论返回码如何)。

You would fail the check if a.batdid not exist. CALLis the only way I know to pull in the environment from a.bat.

如果a.bat不存在,您将无法通过检查。CALL是我所知道的从a.bat.