Batch command teaching compound statement connectors (&, && and | | ) _dos/bat

Source: Internet
Author: User
Tags error code

Help information:

Copy Code code as follows:

& [...] Command1 & Command2 is used to separate multiple commands from one command line. Cmd.exe runs the first command, and then runs the second command.
&& [...] command1 && Command2 is used to run the command following the symbol && only if the previous command succeeds. Cmd.exe runs the first command and then runs the second command only if the first command succeeds.
|| [...] Command1 | | Command2 only in Symbols | | When the previous command fails, it is used to run the symbol | | The following command. Cmd.exe runs the first command and then runs the second command only if the first command fails to run successfully (receive an error code greater than 0).

All of these are official help information (quoted in Ntcmds.chm) and it's best to use GOOGL search to download one.

Command summary:
& generally read "and".

Parameter detailed:

1. First Talk about &

"To separate multiple commands in a command line", the official explanation is actually from the interpreter's point of view. If it is a person's point of view, it should be "connecting more than one command."

For example, I'm going to output hello and world 2 lines of text and connect two echo commands with "&".

Copy Code code as follows:

@echo off
echo Hello & Echo World
Pause

The most common problem with "&" in use is variable latency, which you can refer to in the batch for statement from getting started to mastering the section on this knowledge point.

2. Again, errorlevel.

ERRORLEVEL is made up of two words, one error (error), one level (rank), if you combine these two words, the literal meaning is "error quantity" or "wrong type".

If the command executes successfully, then "error Quantity" is naturally zero. If execution fails, then the "Number of errors" is Non-zero.

About ERRORLEVEL's post:

Effect of Batch internal command on error return code ERRORLEVEL

Some speculations on the ERRORLEVEL in batch processing

3. The command to execute the symbol before the execution of the symbol

Output Hello and World 2 lines of text, connect two echo commands with "&&".

Copy Code code as follows:

@echo off
echo Hello && echo World
Pause

The result is exactly the same as the "&" link two commands, so is the "&" and "&&" functionality?

The answer is: their functions are completely different, and the same result is just a coincidence.

The role of "&" is simply to link multiple commands, and "&&" is to determine whether the command before the symbol is executed successfully, and thus the command after the symbol is executed.

Here is an example to illustrate:

Copy Code code as follows:

C:\users\helloworld\desktop>start "" "111.txt" && echo Hello
The system could not find the file 111.txt.

C:\users\helloworld\desktop>start "" "111.txt" & Echo Hello
The system could not find the file 111.txt.
Hello

Opening a nonexistent file with the Start command, the start command failed because the file was not present, and the "&&" link command "echo Hello" was not executed, and the "&" link was executed.

This feature of "&&" makes it a special conditional operator.

4. Command after symbol execution failure

&& the negative operation is | |
For example:

Copy Code code as follows:

C:\users\helloworld\desktop>start "" "111.txt" | | echo Hello
The system could not find the file 111.txt.
Hello

The above mentioned is the entire content of this article, I hope you can enjoy.

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.