简体   繁体   English

在没有 CALL 的情况下从另一个批处理脚本调用带有 LABEL 的批处理脚本

[英]Calling a batch script with LABEL from another batch script without CALL

I found an interesting feature of a cmd batch script.我发现了一个 cmd 批处理脚本的有趣功能。 The question is: Is it a documented feature, or a bug?问题是:它是记录在案的功能还是错误? (You know, an undocumented unexpected feature is a bug :-) ) (您知道,未记录的意外功能是一个错误 :-) )

For start, I ensure you that I understand the distinction between calling a script using CALL and without CALL:首先,我向您保证我了解使用 CALL 和不使用 CALL 调用脚本之间的区别:

Script.bat
call Script.bat

Now, I have a batch library tools.bat :现在,我有一个批处理库tools.bat

echo tools.bat ARGS: %1 %2 %3 %4
set LABEL=%1
shift /1
goto %LABEL%

:A
echo A ARGS: %1 %2 %3
goto :eof
:B
echo B ARGS: %1 %2 %3
goto :eof

I call it from another script:我从另一个脚本调用它:

@echo off
call :A 1 2 3
call :B 4 5 6
exit /b

:A
:B
tools.bat %*

The strange, but potentially useful, behaviour is that the last line jumps directly to the label in tools.bat , not to the beginning of tools.bat .奇怪,但可能是有用的,行为是最后一行直接跳转到标签中tools.bat ,而不是一开始tools.bat When I replace the last line with a CALL, I would have to rewrite the script substantially, because with CALL this unexpected behaviour does not work:当我用 CALL 替换最后一行时,我将不得不大量重写脚本,因为使用 CALL 这种意外行为不起作用:

@echo off
call :A 1 2 3
call :B 4 5 6
exit /b

:A
call tools.bat :A %*
goto :eof
:B
call tools.bat :B %*
goto :eof

So again, is this feature documented or not?那么,这个功能是否记录在案? It works both in WIn7 and Win10.它适用于Win7和Win10。

This is a known 'hack' but not documented by microsoft.这是一个已知的“黑客”,但没有被微软记录。 One of the possible usages is a creating a 'libary' script that where you can call a specific function -> https://stackoverflow.com/a/30170342/388389一种可能的用法是创建一个“库”脚本,您可以在其中调用特定函数 -> https://stackoverflow.com/a/30170342/388389

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM