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

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

问题描述

我试图了解 GOTO :EOF 返回到代码中的哪个位置?

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

代码如下:

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 是 Microsoft 在命令 转到.在命令提示符窗口中运行的帮助输出 goto/? 也解释了 文件结束 的这个特殊标签.但只有在默认情况下启用命令扩展时才支持此预定义标签.

: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.

在命令提示符窗口中运行的帮助输出 call/? 当然还有命令的文档 CALL 解释了 goto :EOF 应该用于退出用 call 调用的子例程:标签.

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.

一个子程序只不过是嵌入在当前批处理文件中的另一个批处理文件,用命令 call 调用.如果子程序在批处理文件的末尾,则真正的文件结尾标志着子程序的结束.

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.

因此命令解释器需要一个命令来在到达命令处理中的特定行时退出子例程并返回到调用命令行.goto :EOFexit/B 都可以在任何地方使用,以退出子程序或退出当前批处理文件.

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.

在有问题的批处理代码中,第一个 goto :EOF 需要退出批处理文件处理,而不会出现不必要的子程序代码循环结束后.

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.

提问者批处理代码中的第二个 goto :EOF 用于退出子程序并在FOR中继续处理> 在第二行循环.不退出批处理文件的处理,只退出子程序的处理.

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.

注意 1: goto EOF 不带冒号要求批处理文件中确实有一行以 :EOF 开头,即标签 EOF 必须存在于文件中.goto :EOF 总是会导致退出子程序/批处理并启用命令扩展,即使批处理文件中有标签 EOF,因为一行以 开头:EOF.

Note 1: goto EOF without a colon requires that there is really a line starting with :EOF in the batch file, i.e. the 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.

注意 2: 命令 EXIT 总是导致退出整个命令进程,独立于调用层次结构,独立于 Windows 命令处理器的启动方式 - 使用参数 /K> 保持 cmd.exe 在打开命令提示符窗口时按使用方式运行,或者在使用 /C关闭双击批处理文件时使用的命令处理完成.因此 exit 没有 /B 应该在批处理文件中明智地使用(最好:从不).

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).

注意 3: exit/B 不适用于禁用命令扩展,如以下代码所示:

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:

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

The system cannot find the batch label specified - EOF

换句话说,没有附加退出代码的 exit/Bgoto :EOF 完全一样,因此也取决于命令扩展.exit 没有 /B 没有或有退出代码总是有效.

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.

注 4: ERRORLEVEL 不受 goto :EOF 的影响,但 Microsoft GOTO 文档对此主题保持沉默.exit/B #ERRORLEVEL 设置为 # 如 Microsoft 记录的.exit/B # 也可以用来代替 goto :EOF 退出子程序,并在命令行上评估特定的退出代码,调用子程序就像使用运算符 &&|| 或在使用 if errorlevel X 调用命令行后的下一个命令.但是,通常不需要显式退出批处理文件或具有特定退出代码的子程序,因为 goto :EOFexit/B 都不会修改 的当前值错误级别.

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.

注意 5: 不要在命令 GOTO 之间没有空格的批处理文件中使用 goto:EOFcall:Label 分别是CALL(参数0)和标签(参数1).应该始终使用 goto :EOFcall :Label 并在命令和标签之间使用空格作为参数字符串分隔符.原因是 goto:EOF 导致尝试在当前目录中首先查找名称为 goto: 的文件,然后查找名称为 goto:EOF<的文件/代码>.不正确的命令 call:Label 会导致搜索名称为 call: 且下一个名称为 call:Label 的文件.对于这两个语法错误的命令,文件系统两次返回 cmd.exe 名称无效.然后 cmd.exe 检测冒号作为无效名称的原因,并将命令拆分为命令和标签参数,最后成功运行命令.goto :EOFcall :Label 的使用不会导致任何错误的文件系统访问,因为 cmd.exe 会立即识别字符串 goto 分别作为内部命令call.

Note 5: Do not use goto:EOF or call:Label in a batch file with no space between command GOTO respectively CALL (argument 0) and the label (argument 1). There should be always used goto :EOF and call :Label with a space as argument string separator between command and label. The reason is that goto:EOF results in the attempts to find in current directory first a file with name goto: and next a file with name goto:EOF. The incorrect command call:Label results in searching for a file with name call: and next with name call:Label. The file system returns for both syntactically wrong commands twice to cmd.exe that the name is invalid. Then cmd.exe detects the colon as reason for the invalid name and splits the command up into command and label argument and finally runs the command with success. The usage of goto :EOF and call :Label does not cause any wrong file system accesses as cmd.exe immediately recognizes the string goto respectively call as internal command.

有关ERRORLEVEL 行为的详细信息,请参阅:

For details on ERRORLEVEL behavior see:

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

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