GOTO:EOF返回到哪里? [英] Where does GOTO :EOF return to?

查看:508
本文介绍了GOTO:EOF返回到哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解代码中的哪个部分确实是 GOTO:EOF 返回到?



是代码:

$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ .txt)DO(call:子程序%% G)
GOTO:EOF
$ b $:子程序
echo%count%:%1
set / a count + = 1
GOTO:EOF


解决方案

:EOF 是微软在命令 GOTO 。通过在命令提示符窗口 goto /?中运行帮助输出也解释了文件结束的特殊标签。但是这个预定义的标签只有在缺省情况下启用了命令扩展才被支持。

在命令提示符窗口中运行帮助输出 call /? 当然还有命令的文档 CALL < a>解释 goto:EOF 应该用于调用:Label 。

的子程序>

一个子程序就是用命令 call 调用的当前批处理文件中嵌入的另一个批处理文件。如果子程序在批处理文件的末尾,文件的实际结束标志着子程序的结束。



但是批处理文件中可能有多个子程序。因此,命令解释器需要一个命令来在命令处理中到达特定行时退出子例程并返回到调用命令进程。 goto:EOF 以及 exit / B 可以在任何地方用于退出子程序或退出当前批次在批处理代码中,第一个 goto:EOF 是需要的,以便处理。

退出批处理文件处理完成循环后,不会有不必要的子程序代码。 第二 <提问者批处理代码中的code> goto:EOF 用于退出子程序,并在第二行的 FOR 循环中继续处理。它不退出批处理文件的处理,只退出子程序。



注1: goto EOF 没有冒号需要在批处理文件中有一行以:EOF 开始,即跳转标签 EOF 必须存在于文件中。即使在标签 EOF 中, goto:EOF 总是会导致退出子例程/批处理。批处理文件,因为以开始的行:EOF


$ b 注2:命令
EXIT 不带参数 / B 的结果总是在退出独立于调用层次结构的整个命令处理上,而与参数 / K 保持在打开命令提示符窗口时打开)或在命令处理完成后双击批处理文件时使用 / C 关闭)。所以 exit 没有 / B 应该在批处理文件中明智地使用(best:never)。 b
注意3: exit / B / b>

  @echo off 
setlocal DisableExtensions
echo使用命令exit / B禁用命令扩展名。
exit / B

从命令提示窗口执行这个批处理文件,错误信息:

lockquote

系统找不到指定的批次标签 - EOF



换句话说 exit / B 完全像 goto:EOF 也在命令扩展上。 exit without / B 始终有效。

注意4 ERRORLEVEL 不受 goto:EOF 的影响,但Microsoft GOTO 文档在这个主题上是静音的。 exit / B# sets ERRORLEVEL to 。但是,通常不需要显式退出具有特定退出码的批处理文件,因为 goto:EOF 也不是 exit / B 修改 ERRORLEVEL 的当前值。

有关 ERRORLEVEL 行为见:


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

解决方案

: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 process. goto :EOF as well as exit /B can be both used everywhere to either exit a subroutine or exit the current batch 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 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 processing independent on calling hierarchy and independent on how the command interpeter was started with parameter /K (keep open as used when opening a command prompt window) or with /C (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 is exactly like goto :EOF and depends therefore also on command extensions. exit without /B 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. However, explicitly exiting a batch file 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:

这篇关于GOTO:EOF返回到哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆