Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calling a nested batch file without prepending "call" to the line exit the parent batch file?

Tags:

batch-file

cmd

I understand how to call nested batch files from within a parent file using the call command, as there are plenty of resources on that:

  • CALL
  • CALL (SS64)
  • Bat file termination

However, I don't understand why calling another batch file from another terminates the parent.

For a less abstract example, suppose I have a batch file that "links" together separate batch files, and I erroneously didn't prepend call to each line:

foo.bat bar.bat 

This would only execute foo.bat and then exit. To correctly execute both commands, I would have to prepend call before each statement:

call foo.bat call bar.bat 

Why does the first functionality still exist? Why hasn't it been changed? I noticed that call was introduced in MS-DOS 3.3, which was released in the late 1980s, so is this functionality still here for reverse compatibility?

I can't think of any (practical) usages of it, but perhaps I'm too used to "new" programming techniques.

like image 604
Brandon Amos Avatar asked Jul 24 '12 20:07

Brandon Amos


People also ask

How do I stop a batch file from exiting?

You can insert the pause command before a section of the batch file that you might not want to process. When pause suspends processing of the batch program, you can press CTRL+C and then press Y to stop the batch program.

Why do batch files start with @echo off?

If used in a batch file, echo on and echo off don't affect the setting at the command prompt. To prevent echoing a particular command in a batch file, insert an @ sign in front of the command. To prevent echoing all commands in a batch file, include the echo off command at the beginning of the file.

What is exit in batch file?

EXIT /B at the end of the batch file will stop execution of a batch file. use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.


2 Answers

DOS used simple text processing (back when you had things like FILES=20 in config.sys to allow 20 file handles), so opened the file, read the next line, closed the file, then executed the line just read. If the file called another, then the processing continued with that file, so only 1 file handle would be required for a batch file.

Until Microsoft put in the call command, there was no way to get back to the original file (without using tricks like giving the name of the previous file as a parameter, and using temporary files to let the original batch file know it had dome some processing, and could then GOTO the next part of the file).

like image 95
SeanC Avatar answered Oct 06 '22 00:10

SeanC


As Sean Cheshire wrote, it's necessary for backward compatibility.

But starting a batch file from a batch file without using CALL does not terminate the parent!
It looks that way, as the parent normally will not executed further after the second batch exits.
But using a call before starting the second.bat, will show that the first batch isn't terminated.

parent.bat

echo parent.bat call :myLabel echo back in parent.bat main exit /b  :myLabel second.bat & echo back in parent.bat exit /b 

second.bat

echo second.bat exit /b 

I use here the the secpond.bat & echo back ... to avoid another bug/feature of cmd.exe.
If you use second.bat without any extras it will start second.bat AND jump to the label :myLabel in second.bat!

like image 21
jeb Avatar answered Oct 05 '22 22:10

jeb