简体   繁体   English

Windows命令提示符上的'&&'似乎忽略了错误代码

[英]Error codes seemingly ignored by '&&' on Windows command prompt

I have some Batch scripts I use for automating application build processes, most of which involve chaining commands together using the && operator. 我有一些用于自动执行应用程序构建过程的批处理脚本,其中大多数涉及使用&&运算符将命令链接在一起。 Admittedly, I'm more experienced with Linux, but based on that experience some_command && other_command should result in other_command being run iff some_command returns an exit code of 0 . 诚然,我对Linux有更丰富的经验,但是基于该经验, some_command返回退出代码0时, some_command && other_command应该导致other_command运行。 This answer and this answer seem to agree with that. 这个答案这个答案似乎是一致的。 However this appears not to be the case on Windows cmd.exe, all of the scripts run regardless of the error code of the previous. 但是,在Windows cmd.exe上似乎不是这种情况,所有脚本都会运行,而与先前的错误代码无关。

I decided to make a simple test for this to convince myself I wasn't going insane. 我决定对此进行一个简单的测试,以使自己确信自己不会发疯。 Consider this test.bat , which returns an exit code of 1: 考虑一下这个test.bat ,它返回退出代码1:

@echo off
EXIT /B 1

Running test.bat && echo This shouldn't print prints 'This shouldn't print'. 运行test.bat && echo This shouldn't print打印'这不应该打印'。 But since the exit code is clearly 1, echo should not be called. 但是由于退出代码显然为1,因此不应调用echo I've tested that the error code was actually 1 using the %errorlevel% variable, they're coming out as expected (0 before I run the script, 1 after). 我已经使用%errorlevel%变量测试了错误代码实际上是1,它们按预期方式输出(在运行脚本之前为0,在运行脚本之后为1)。

On Linux I tried the same thing. 在Linux上,我尝试过同样的事情。 Here's test.sh : 这是test.sh

#!/bin/bash
exit 1

Running ./test.sh && echo "This shouldn't print" gives no output, exactly what I expected. 运行./test.sh && echo "This shouldn't print"给出任何输出,正是我所期望的。

What's going on here? 这里发生了什么?

( Note: OS is Windows 7 Enterprise) 注意:操作系统是Windows 7 Enterprise)

You need to use call to run the batch script, like this: 您需要使用call来运行批处理脚本,如下所示:

call test.bat && echo This shouldn't print

Without call , the && operator does not receive the ErrorLevel returned by the batch script. 如果不call ,则&&运算符不会收到批处理脚本返回的ErrorLevel


When you run a batch file from within another one, you need to use call in order to return to the calling batch file; 当您从另一个内部运行一个批处理文件时,您需要使用call才能返回到正在调用的批处理文件; without call , execution terminates as soon as the called batch file has finished...: call ,执行将在被调用的批处理文件完成后立即终止...:

call test.bat
echo This is going to be displayed.

...but: ...但:

test.bat
echo You will never see this!

When running test.bat is involved in a command line where multiple commands are combined (using the concatenation operator & , the conditional ones && and || , or even a block of code within parentheses () ), all the commands following test.bat are ecexuted even if call was not used. 当在组合了多个命令的命令行中运行test.bat (使用串联运算符& ,条件运算符&&||甚至是括号()的代码块), test.bat所有命令即使未使用call ,也会被覆盖。 This is because the entire command line/block has already been parsed by the command interpreter. 这是因为命令解释器已经解析了整个命令行/块。

However, when call is used, the ErrorLevel value returned by the batch file is received (which is 1 in our situation) and the following commands behave accordingly: 但是,使用call时,会收到批处理文件返回的ErrorLevel值(在我们的情况下为1 ),并且以下命令将相应地起作用:

call test.bat & echo This is always printed.
echo And this is also always printed.

call test.bat && echo This is not printed.

call test.bat || echo But this is printed.

(
    call test.bat
    echo This is printed too.
    echo And again this also.
)

call test.bat & if ErrorLevel 1 echo This is printed.

But without call you will get this...: 但是没有call你会得到这个...:

test.bat & echo This is printed.
echo But this is not!

...and...: ...和...:

test.bat && echo Even this is printed!
echo Neither is this!

...and...: ...和...:

test.bat || echo But this is not printed!
echo And this is not either!

...and: ...和:

(
    call test.bat
    echo This is printed.
    echo And this as well.
)

It seems that the && and || 似乎&&|| operators receive an ErrorLevel of 0 -- even in case ErrorLevel has already been set before test.bat is executed, strangely. 操作员收到的ErrorLevel0奇怪的是,即使在执行test.bat之前已经设置了ErrorLevel也很奇怪。 Also when if ErrorLevel is used, the behaviour is similar: 同样, if ErrorLevel ,则行为类似:

test.bat & if ErrorLevel 1 echo This is not printed!

...and...: ...和...:

set = & rem This constitutes a syntax error.
test.bat & if ErrorLevel 1 echo This is still not printed!

Note that the commands behind test.bat execute after the batch script, even without call . 请注意, test.bat后面的命令在批处理脚本之后执行,即使没有call

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM