简体   繁体   中英

Calling a batch script with LABEL from another batch script without CALL

I found an interesting feature of a cmd batch script. 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:

Script.bat
call Script.bat

Now, I have a batch library 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 . 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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