Where does GOTO :EOF return to?

2018-12-31 04:07发布

I'm trying to understand where in the code exactly does GOTO :EOF return to?

Here is the code:

SET count=1 
FOR /f "tokens=*" %%G IN (somefile.txt) DO (call :subroutine "%%G") 
GOTO :EOF

:subroutine  
echo %count%:%1  
set /a count+=1  
GOTO :EOF

3条回答
梦寄多情
2楼-- · 2018-12-31 04:30

:EOF is a predefined label as Microsoft explains in documentation for command GOTO. The help output by running in a command prompt window goto /? explains also this special label for End Of File. But this predefined label is supported only with command extensions being enabled as by default.

The help output by running in a command prompt window call /? and of course also the documentation for command CALL explain both that goto :EOF should be used to exit a subroutine called with call :Label.

A subroutine is nothing else than another batch file embedded within current batch file called with command call. If the subroutine is at end of the batch file, real end of file marks the end of the subroutine.

But there can be multiple subroutines in a batch file.

So a command is needed for command interpreter to exit the subroutine on reaching a specific line in command processing and go back to the calling command line. goto :EOF as well as exit /B can be both used everywhere to either exit a subroutine or exit the current batch file processing.

In batch code in question the first goto :EOF is needed to exit batch file processing without an unwanted fall through to the subroutine code after finishing the loop.

The second goto :EOF in batch code of questioner is for exiting the subroutine and continue processing in FOR loop in second line. It does not exit processing of the batch file, it exits only the processing of the subroutine.

Note 1: goto EOF without a colon requires that there is really a line starting with :EOF in the batch file, i.e. the jump label EOF must exist in the file. goto :EOF always results in exiting subroutine/batch processing with command extensions enabled even if there is a label EOF in the batch file because of a line starting with :EOF.

Note 2: Command EXIT without parameter /B results always in exiting entire command process independent on calling hierarchy and independent on how the Windows command processor was started – with parameter /K to keep cmd.exe running as used when opening a command prompt window or with /C to close after command processing finished as used on double clicking a batch file. Therefore exit without /B should be used wisely in a batch file (best: never).

Note 3: exit /B does not work with command extensions disabled as demonstrated by this code:

@echo off
setlocal DisableExtensions
echo Use command exit /B with command extensions disabled.
exit /B

Executing this batch file from within a command prompt window results in output of the error message:

The system cannot find the batch label specified - EOF

In other words exit /B without an additional exit code is exactly like goto :EOF and depends therefore also on command extensions. exit without /B without or with an exit code works always.

Note 4: ERRORLEVEL is not affected by goto :EOF, but the Microsoft GOTO documentation is mute on this topic. exit /B # sets ERRORLEVEL to # as documented by Microsoft. exit /B # can be also used instead of goto :EOF to exit a subroutine with a specific exit code evaluated on the command line calling the subroutine like on using the operators && or || or on next command after calling command line with if errorlevel X. However, explicitly exiting a batch file or a subroutine with a specific exit code is usually not needed as neither goto :EOF nor exit /B modify the current value of ERRORLEVEL.

For details on ERRORLEVEL behavior see:

查看更多
与君花间醉酒
3楼-- · 2018-12-31 04:37

:eof means "End of file". It's used to make the script finish without executing any of the commands below.

查看更多
看风景的人
4楼-- · 2018-12-31 04:39

GOTO :EOF is functionally equivalent to exit /B, but both forms only works when Extensions are enabled. The test for this point is very simple:

setlocal DisableExtensions
goto :EOF

Compare previous code vs. this one:

setlocal DisableExtensions
exit /B

This means that GOTO :EOF returns to the same point where exit /B would return.

查看更多
登录 后发表回答