Linux Pipeline Symbols | , as well as && | | Wait, special symbol notes.

Source: Internet
Author: User

Piping and re-orientation: "|", ">", ">>", "<"

Re-orientation is to make the command change the standard output it determines. ">" can output the results to a file, the original content of the file will be deleted, ">>" will append the results to the file, the original file content will not be deleted. "<" can change the standard input. Such as:
Cat Data1.txt>>data2.txt (Add the contents of the Data1.txt file to the back of the Data2.txt file)
Pipeline "|" The result of the command can be output to another command as input:
Man Mtools|grep mbadblocks (search for sentences containing "mbadblocks" in Mtools's help)

Man mtools|less (the output pipe is imported into a tool called less.) Less is a paging tool that allows you to view information on a page-by-page. )



Connection symbol: ";"

When there are several commands to be executed in succession, we can put them in one line, in the middle with ";" Separate.
mkdir Myfile;cp/tmp/myfile.txt myfile (first create a directory myfile and then copy the MyFile.txt to the new directory)


Background execution: "&"

Users sometimes take a long time to execute commands, which can affect other things. The best way to do this is to put it in the background. Programs running in the background can continue to execute after the user logs off. When you want to place the command in the background, add "&" after the command.






Linux Shell && | |


When the shell executes a command, it returns a return value that is saved in the shell variable $? In When $? = = 0 o'clock, indicating successful execution; = = 1 o'clock, which indicates that the execution failed.
Www.2cto.com
Sometimes the next command depends on whether the previous command succeeds. For example, execute another command after executing a command successfully, or execute another command after a single command fails. The shell provides && | | To implement command execution control, the shell will be based on && or | | The return value of the preceding command to control the execution of subsequent commands.

&& (Command execution control)


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.
Www.2cto.com
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.

|| (Command execution Control)


The syntax format is as follows:

Command1 | | Command2 [| | Command3 ...]

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 the file 1.txt exists in the ~/desktop directory, the success prompt is output, otherwise the fail prompt message is output.







The shell provides two methods (() and {}) implementations to execute together in conjunction with several commands, instead of being executed independently. This method does not control whether the command needs to be executed, only the combination of multiple individual commands, and the return value of the final command is determined by the return value of the last command.




() (command combination)


The syntax format is as follows: www.2cto.com

(Command1;command2[;command3 ...])

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.

Linux Pipeline Symbols | , as well as && | | Wait, special symbol notes.

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.