Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CALL for labels in a batch script

When using the CALL command to call a label in a batch script, and you end the sub-routine with GOTO:eof, what happens from there? Does it return back to where the sub-routine's CALL is located? Or does it continue on after the location of the call script?

For example:

ECHO It's for my college fund.
CALL :OMGSUB
ECHO *runs away and cries like a little girl*

:OMGSUB
ECHO Your mom goes to college.
GOTO:eof

ECHO *picks up jewelry box*

After GOTO:eof which line will it echo next?

like image 726
Anthony Miller Avatar asked Jul 18 '11 18:07

Anthony Miller


People also ask

What is label in batch script?

Labels in Batch Files. Labels normally mark the beginning of a code block that is the target of a GOTO instruction, but they can also be used for comments. Labels begin with ':' and contain up to eight characters - anything beyond the first eight will be ignored, both in the label and in the GOTO command.

What does %% mean in batch script?

Represents a replaceable parameter. Use a single percent sign ( % ) to carry out the for command at the command prompt. Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> )

How do you call a batch file from another batch file with parameters?

@ppumkin: You can call my batch file from another and pass the parameters on the call line. Your approach seems to be to call the batch file from another and pass the parameters by setting environment variables. This is also a valid approach, mine is just an alternative I like to use and propose.


1 Answers

Why not just run it and see for yourself? I saved your script to a batch file called foo.bat, and I changed the Your mom goes to college. to have an echo in front.

C:\temp>foo

C:\temp>ECHO It's for my college fund.
It's for my college fund.

C:\temp>CALL :OMGSUB

C:\temp>echo Your mom goes to college.
Your mom goes to college.

C:\temp>GOTO:eof

C:\temp>ECHO *runs away and cries like a little girl*
*runs away and cries like a little girl*

C:\temp>echo Your mom goes to college.
Your mom goes to college.

C:\temp>GOTO:eof

C:\temp>

So it's easy to see that after the OMGSUB is called, it

  1. Goes to the end of file.
  2. Then it returns to the line right afer the CALL :OMGSUB and echos the "runs away" line
  3. Then it echos the Your Mom line again
  4. Then it goes to end of file and terminates
  5. The line echo *picks up jewewlry box* never gets reached.
like image 54
dcp Avatar answered Oct 08 '22 16:10

dcp