0

CMD && and || operators work as expected when I use node.js to set ERRORLEVEL:

Microsoft Windows [Version 10.0.19044.1645]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Blaine>node -e "process.exit(0)"

C:\Users\Blaine>echo %ERRORLEVEL%
0

C:\Users\Blaine>node -e "process.exit(1)"

C:\Users\Blaine>echo %ERRORLEVEL%
1

C:\Users\Blaine>node -e "process.exit(2)"

C:\Users\Blaine>echo %ERRORLEVEL%
2

C:\Users\Blaine>node -e "process.exit(0)" && echo expected
expected

C:\Users\Blaine>node -e "process.exit(1)" || echo expected
expected

C:\Users\Blaine>node -e "process.exit(2)" || echo expected
expected

C:\Users\Blaine>node -e "process.exit(0)" || echo UNexpected

C:\Users\Blaine>node -e "process.exit(1)" && echo UNexpected

C:\Users\Blaine>node -e "process.exit(2)" && echo UNexpected

C:\Users\Blaine>

But behavior is different and unexpected when I use CMD scripts 'exit0.cmd', 'exit1.cmd, 'exit2.cmd' which are:

@echo off
exit /b 0

and

@echo off
exit /b 1

and

@echo off
exit /b 2

ERRORLEVEL gets set exactly the same as for the node.js executions:

C:\Users\Blaine\tmp>exit0

C:\Users\Blaine\tmp>echo %ERRORLEVEL%
0

C:\Users\Blaine\tmp>exit1

C:\Users\Blaine\tmp>echo %ERRORLEVEL%
1

C:\Users\Blaine\tmp>exit2

C:\Users\Blaine\tmp>echo %ERRORLEVEL%
2

but && and || don't work right for non-0 ERRORLEVELS:

C:\Users\Blaine\tmp>exit0 && echo expected
expected

C:\Users\Blaine\tmp>exit0 || echo UNexpected

C:\Users\Blaine\tmp>exit1 && echo UNexpected
UNexpected

C:\Users\Blaine\tmp>exit1 || echo expected

C:\Users\Blaine\tmp>exit2 && echo UNexpected
UNexpected

C:\Users\Blaine\tmp>exit2 || echo expected

C:\Users\Blaine\tmp>

What the hell is going on?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Blaine
  • 1,577
  • 2
  • 17
  • 15
  • 4
    Please read the comments below the answer on [What are the ERRORLEVEL values set by internal cmd.exe commands?](https://stackoverflow.com/a/34987886/3074564) The comments written by [dbenham](https://stackoverflow.com/users/1012053/dbenham) and [Aacini](https://stackoverflow.com/users/778560/aacini) could be enlightening. You would get the expected result on using `call exit1.cmd && echo UNexpected || echo expected` because of the usage of command __CALL__ the exit value of the batch file processing is immediately copied to dynamic variable `ERRORLEVEL` of `cmd` before its evaluation. – Mofi Apr 24 '22 at 10:52
  • The behavior of `exit1.cmd && echo UNexpected || echo expected` is a bug of `cmd.exe` in my opinion like a few other minor bugs of `cmd.exe` in some rare use cases. Unexpected behavior caused by a bug in code of an executable are always hard to explain as the behavior is not by design, but caused by a mistake in the code. – Mofi Apr 24 '22 at 10:55
  • Thanks Mofi. This is pretty awful behavior. I'm working on discovery scripts that have to be bullet-proof. I guess I have to use "CALL" for every dynamic command invocation where the invoked command could be a CMD script. – Blaine Apr 24 '22 at 14:20

0 Answers0