Shell in && | | How to use

Source: Internet
Author: User


&& operators:

Command1 && Command2

&& the command on the Left (command 1) returns True (that is, returns 0, executed successfully) and the command (Command 2) on the right side of the,&& can be executed, in other words, "If this command succeeds && executes this command."

The syntax format is as follows:

Command1 && command2 [&& Command3 ...]

1 use && connection between commands to implement logic and functions.

2 only the command on the left of && returns True (command return value $?). = = 0) The command to the right of,&& will be executed.

3 As long as there is a command return False (command return value $?) = = 1), the subsequent command will not be executed.

Example 1

[Email protected]:~$ cp ~/desktop/1.txt ~/1.txt && RM ~/desktop/1.txt && echo "Success"

The command in Example 1 first copies the 1.txt file from the ~/desktop directory to the ~ directory; After successful execution, the source file is deleted by using RM, and if the deletion succeeds, the message is output.


|| Operator:

Command1 | | Command2

|| is the opposite of &&. if | | The command on the Left (Command 1) did not execute successfully, then execute | | The command on the right (command 2), or in other words, "If this command fails to execute | | Then execute this command.

1 Use between commands | | A connection that implements logic or functionality.

2 Only in | | The left command returns False (the command returns a value of $?). = = 1), | | The command on the right will not be executed. This is the same as the logical or syntactic function in C, which is to implement short-circuit logic or operations.

3 As long as there is a command return True (command return value $?) = = 0), the subsequent command will not be executed.

Example 2

[Email protected]:~$ rm ~/desktop/1.txt | | echo "Fail"

In Example 2, if file 1.txt does not exist under the ~/desktop directory, the prompt message is output.

Example 3

[Email protected]:~$ rm ~/desktop/1.txt && echo "Success" | | echo "Fail"

In Example 3, if file 1.txt is present in the ~/desktop directory, the success prompt is output, otherwise the output fail



Prompt information.

If you want to execute several commands together, the S H e l L provides two methods. A set of commands can be executed either at the current S H e l l or in the child S H e l L.

() Operator:

In order to execute a set of commands in the current shell, you can separate each command with a command delimiter (that is, ",") and enclose all the commands in parentheses ().

The general form of it is:

(Order, command, order, order, etc.)

11 commands require exclusive one physical line, and if multiple commands need to be placed on the same line, the command delimiter (;) is used to separate the commands. The effect is equal to the effect that multiple independent commands perform separately.

2 () indicates that multiple commands are executed as a whole in the current shell. It is important to note that commands that are enclosed in () do not switch the current working directory before execution, which means that the command combination is executed in the current working directory, although there are commands to switch directories in the command.

The 3 command combination is often used in conjunction with command execution control.

Example 4

[Email protected]:~$ rm ~/desktop/1.txt | | (CD ~/desktop/;ls-a;echo "fail")

In Example 4, if file 1.txt does not exist under directory ~/desktop, a combination of commands is executed.

{} operator:

If {} is used instead of (), then the corresponding command will be executed as a whole in the child shell instead of the current shell, and only if the output of all commands in {} is redirected as a whole, where the command is executed in the child shell, otherwise in the current shell.

The general form of it is:

{command, command, command ...}

(Note: You must use a space between {} and the command when using {})

Test:

~$ A=1;echo $A; {a=2;}; Echo $A

1

2

~$ A=1;echo $A;( a=2; ); Echo $A

1

1



This article is from the "Cold River Exclusive Fishing" blog, please be sure to keep this source http://iter2012.blog.51cto.com/6873825/1613705

Shell in && | | How to use

Related Article

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.