How to properly report an exit status in batch?

后端 未结 5 1455
[愿得一人]
[愿得一人] 2020-12-03 13:08

I\'m facing a weird situation where a batch file I wrote reports an incorrect exit status. Here is a minimal sample that reproduces the problem:

bug.cmd

5条回答
  •  一整个雨季
    2020-12-03 13:29

    @dbenham's answers are good. I am not trying to suggest otherwise. But, I have found it reliable to use a variable for the return code and a common exit point. Yes, it takes a few extra lines, but also allows additional cleanup that, if necessary, would have to be added to every exit point.

    @ECHO OFF
    SET EXITCODE=0
    
    if "" == "" (
            echo first if
            set EXITCODE=%ERRORLEVEL%
            GOTO TheEnd
    
            if "" == "" (
                    echo second if
            )
    )
    
    :TheEnd
    EXIT /B %EXITCODE%
    

提交回复
热议问题