Windows Batch learning (call and start)---02

Source: Internet
Author: User
Tags echo command exit in tag name

Reference: https://www.cnblogs.com/Braveliu/p/5078283.html

I. Summary of the callcommand

(1) About the call command

Syntax: call [[drive:] [Path] FileName [batchparameters]] [: Label [arguments]]

Parameter: [Drive:][path] FileName Specifies the location and name of the batch program to invoke. The Filename parameter must be a type file for the. bat or. cmd extension.

batchparameters Specifies the command line information (that is, the parameter item) required by the batch program.

Call another batch program without terminating the parent batch program (if you call another batch file without calling, you will not be able to return the current file and execute subsequent commands for the current file after executing the batch file).

The call command accepts a label that is used as a jump target. If you use call outside of a script or batch file, it will not work at the command line.

(2) Call command application

Test1: Create a new two text file, one named Call1.bat.

1 echo running in CALL1

Another named Call2, modify the file type to bat, and open the edited content with notepad++:

1 @echo off2 echo start3 call Call1.bat4 echo running in call25 echo End6 pause

  

Note: The first line of the call1 script does not write the @echo off statement. Because the first line of Call2 has turned off the command echo state, this setting is also valid when Call2 calls CALL1, which allows for a deeper understanding of the invocation.

Test2: Call tag.

1 @echo off2 call:label3 echo Echo 6:label7 echo 4 echo

Another name is CALL4

1 @echo off2 Call CALL3.BAT3 Pause

Double-click the Execute CALL4 file, and the execution results are as follows:

TEST3: Calling a batch with parameters

1 echo%1%22 echo%3

Another named Call8, modify the file type to bat, and open the edited content with notepad++:

1 @echo off2 call call7.bat Hello world!3 dir c:4 Pause

(3) The difference between the call command and the goto command

Application Example 2 of the call command, we see the Invoke command's invocation of the jump tag, note that the call must be preceded by a colon in front of the tag name, and Goto is directly followed by the tag name.

As in the previous example, let's look at the execution effect of Goto: Create a new two text file named Call5

1 @echo off2 goto label3 Echo Echo 6:label7 echo 4 echo

Another named call6, modify the file type to bat, and open the edited content with notepad++:

1 @echo off2 Call CALL5.BAT3 Pause

Double-click the Execute call6 file, and the execution results are as follows:

Ii. Summary of the start command

(1) Introduction to Start command

In the command Prompt window that opens, type start/?--> enter to see the parameters and usage notes for the "Start" command (note: Press any key to see more of what follows) Example:

(2) Start command application

Test1: Basic applications. Open the system calculator and Notepad.

Create a new text file named Start1, modify the file type to bat, and open the edited content with notepad++:

1 start calc2 start Notepad3 exit

Execution results: Open the calculator and Notepad separately, while the terminal flashes past (because of the last exit).

(3) The difference between the start command and the call command

The Start command application example: Create a new two text file, a named Startmain, modify the file type to bat, and open the edits with notepad++:

1 @echo off2 set a=13 pause>nul4 echo start startchild.bat5 start startchild.bat6 echo end Start7 Pause>nul8 Echo%b %9 Pause>nul

A named StartChild, modify the file type to bat, and open the edited content with notepad++:

1  @echo off2 echo%a%3 set b=204 Pause>nul

Double-click the execution startmain.bat to see the results as shown:

For the same application, if you change to the call command: Create a new two text file, a named Callmain, modify the file type to bat, and open the edited content with notepad++:

1 @echo off2 set a=13 pause>nul4 echo call CALLCHILD.BAT5 call Callchild.bat6 echo end Call7 Pause>nul8 Echo%b%9 PA Use>nul

A named Callchild, modify the file type to bat, and open the edited content with notepad++:

1  @echo off2 echo%a%3 set b=204 Pause>nul

Double-click the execution callmain.bat to see the results as shown:

  Analysis results:

In the application example, variable A is defined in Main.bat with a value of 1, and variable b is defined in Child.bat with a value of 20.

Main.bat first executes call Child.bat, then Child.bat will perform Echo%a%, which now displays 1 normally.

After the call is finished, Main.bat will perform the echo%b% and display 20 normally.

Then, if the call is changed to start, after Child.bat execution and then closed, Main.bat continue to echo%b%, will not be able to display the value of variable B, but only the equivalent of executing the echo command, the result is: Echo is turned off.

Call, they have the same shell Cmd.exe, in the same process, so their variables are interoperable.

When start, they have two shell cmd.exe, but Child.bat can be seen as a child of the main.bat, and the child process can read the variables in the parent process (that is, the value of a).

 Summary conclusion:

For start, an overview of two words: "Different processes can not pass value", "the same process one-way transfer value, Lao Zi Chuan son, non-son preach Lao Tzu."

For call, "the same process, variable interoperability."

In addition, the difference between the start and call commands can be understood in the following ways:

One is the invocation scope is different: Call is mainly used for batch processing of internal calls, such as call:p end and some DOS commands such as calls set test=2, but can also invoke other executables, and start cannot make internal calls, but it can do basically all external programs , you can also execute the shell, such as open folder Start "" "%WINDIR%", install network printer start "" \\IP\Printer "and so on.

The other is called in a different way: call is called in the strict sense, while the other batch is executed in the same form process, and start is executed, so the other batch is executed in a different form process, that is, in the newly opened process, Although start can add the B parameter, the results are completely different. If we use the call set test=2 and start/b set test=2 appear to execute the same result, but we found that the latter has two processes, and in the form to execute two exit to exit, So when we use start to execute a batch, it is best to add an exit to the called batch, otherwise we cannot exit the called Batch DOS form, but if there is an exit in the called batch with call, the original and the called batch program will be terminated directly. This is a very serious problem. It is recommended that you use goto:eof in the called batch instead of exit.

Third, the invocation result is different: Call calls can not only pass parameters or variables to the called batch, and the called batch can also set parameters or variables for callbacks, and start can only pass parameters or variables to the called batch, but not the parameters or variables, which is actually the 2nd extension. Also, we must note that when invoking other batches using call, it is recommended to use the Goto command in the called batch if we use a different tag name from the original batch, otherwise it might jump into the original batch and not guarantee the full execution of all the statements in the called batch.

Windows Batch learning (call and start)---02

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.