Linux Shell pipeline command (PIPE)

Source: Internet
Author: User

Pipeline commandThe operator is: "|", which can only process the correct output information transmitted through the previous command, that is, the standard output information. For stdandard
Error information is not directly processed. Then, pass it to the next command as the standard input.

 

  • Pipeline command instructions:

Let's take a look at the figure below:

The correct output of command1 is the input of command2, and the output of comand2 is the input of comand3, and the output of comand3 is directly displayed on the screen.

After passing through the pipeline: The correct output of comand1 and comand2 is not displayed on the screen

Note:

1. Pipeline commands only process the correct output of the previous command, without handling the error output

2. The command on the right of the pipeline command must be able to receive standard input stream commands.

Instance:

 

[Chengmo @ centos5 shell] $ CAT test. Sh | grep-N 'echo '5: Echo "very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; # Read test. sh file content, which is forwarded to grep through a pipeline as the input content [chengmo @ centos5 shell] $ CAT test. sh test1.sh | grep-N 'echo 'cat: test1.sh: No file or directory 5: Echo "very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; # Cat test1.sh does not exist. The error output is printed to the screen. The correct output is sent to grep [chengmo @ centos5 shell] $ CAT test. sh test1.sh 2>/dev/null | grep-N 'echo '5: Echo "very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; # Redirect the error output not found in test1.sh to the/dev/null file. The correct output is sent to grep [chengmo @ centos5 shell] $ CAT test in the pipeline. sh | lscatfile httprequest.txt secure test testfdread. sh testpipe. sh testsh. sh testwhile2.shenvcron.txt Python sh testcase. sh testfor2.sh testselect. sh test.txt text.txtenv.txt release SMS testcronenv. sh testfor. sh test. sh testwhile1.sh # Read test. sh content, which is sent to the LS command through the MPs queue. Because ls does not support standard input, data is discarded.

Here the instance is the verification of the above two points of attention. To receive standard input commands. Otherwise, data will be discarded during the transfer process. Commonly used to receive data pipeline Commands include: SED, awk, cut, Head, top, less, more, WC, join, sort, split, and so on. These are all text processing commands.

  • Differences between pipeline commands and redirection

The difference is:

1. standard output should be provided for the commands on the left. | standard input should be accepted for the commands on the right.
The command on the left should have standard output> only files on the right
Standard input is required for the command on the left <only files on the right

 

2,MPs queueTrigger two sub-processes to execute "|" programs on both sides.RedirectionIs executed in a process

These are all summarized on the Internet. In fact, as long as you know more about the usage, you must have a different description.

Instance:

 
 
# Mutual conversion possible # input redirection [chengmo @ centos5 shell] $ CAT test. Sh | grep-N 'echo '5: Echo "very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; #" | "Both sides of the MPs queue must be the shell command [chengmo @ centos5 shell] $ grep-N 'echo '<test. Sh 5: Echo" very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; #" Redirection "symbol, the right side can only be a file (common file, file descriptor, file device) [chengmo @ centos5 shell] $ mail-s 'test' 8292669@qq.com <test. sh [chengmo @ centos5 shell] $ CAT test. sh | mail-s 'test' 8292669@qq.com # the above two are also the same, the test. SH Content is sent to the specified mailbox. [Chengmo @ centos5 shell] $ (sed-n'1, $ P' | grep-N 'echo ') <test. Sh 5: Echo "very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; # This script is interesting. As the pipeline is in front, the test. Sh content needs to be redirected to SED, and sed output is passed through the pipeline, and input to grep. You need to enclose the above with the "()" operator. Commands in parentheses can be considered as a command. If no brackets are added, test. Sh is the grep input. # The above one is equivalent to the [chengmo @ centos5 shell] $ sed-N '1, $ P' <test. Sh | grep-N 'echo '5: Echo "very good! "; 7: Echo" Good! "; 9: Echo" pass! "; 11: Echo" No pass! "; # Rewrite operator, which is checked first before Shell Command Parsing (a command must check its input and output before execution, that is, whether the 0, 1, and 2 devices are ready ), therefore, the highest priority is [chengmo @ centos5 shell] $ sed-N '1, 10p' <test. sh | grep-N 'echo '<testsh. sh10: Echo $ total; 18: Echo $ total; 21: Echo "OK"; # Haha, this grep accepts pipeline Input and testsh. sh input. Do I receive both of them. As mentioned earlier, the "<" operator takes precedence. Before the MPs queue sends data, grep binds the testsh. Sh input, and the SED command output is discarded. Be careful to use # output redirection [chengmo @ centos5 shell] $ CAT test. sh> test.txt [chengmo @ centos5 shell] CAT test. sh | tee test.txt &>/dev/rewrite, then copy the standard input to the standard output (stdout), So redirect to/dev/null without displaying the output # ">" output redirection, usually on the far right of a command, receive the command on the left, output the result, and redirect to the specified file. You can also use the command center. [Chengmo @ centos5 shell] $ ls test. sh test1.sh testsh. sh 2> err.txt | grep 'test' test. shtestsh. sh # The directory "test" and "testsh" does not exist. Therefore, the LS command is mistakenly output to err.txt for correct output and sent to the grep command through the pipeline. [Chengmo @ centos5 shell] $ ls test. sh test1.sh testsh. sh &> err.txt | grep 'test' # The printed result is empty. & it indicates that both the correct and wrong outputs are input to err.txt, and the data passed to the following through the pipeline is empty, therefore, there is no displayed # similarly ">" output redirection character, and the priority is first parsed. When a command has this character, it will be bound to the standard output on the left. After you have prepared the data, wait for the command to execute the output data and it will start to receive the data.

Summary:

As shown in the above example, redirection and pipelines can be used in many cases. In fact, in shell, they are often used in Rome. Generally, it is better to use the redirection output if the parameters are passed between commands or the pipeline is good. If the output result needs to be redirected to a file, it is better to use the redirection output.

Command Execution sequence: Linux Shell wildcards, metacharacters, and escape characters

 

 

  • Shell script receives pipeline Input

Interesting questions:

Since the pipeline can receive commands and the standard input is needed, can we develop such a basic program using shell scripts? (We often see some system commands as pipeline receivers)

Instance (testpipe. Sh ):

#! /Bin/sh if [$ #-GT 0]; then exec 0 <$1; # determine whether to pass in the parameter: file name. If yes, bind the file to the standard input fi while read line do echo $ line; done <& 0; # Read the content through the standard input loop exec 0 &-; # unbind the standard input

Running result:

[Chengmo @ centos5 shell] $ cat testpipe.txt 1, t, est pipe2, T, est pipe3, T, est pipe4, T, est pipe0000testpipe.txt is only the test text to be read [chengmo @ centos5 shell] $ cat testpipe.txt | sh testpipe. sh1, T, est pipe2, T, est pipe3, T, est pipe4, T, est pipe # Read testpipe.txt through cat and send it to testpipe. sh standard input [chengmo @ centos5 shell] $ sh testpipe. sh testpipe.txt 1, t, est pipe2, T, est pipe3, T, est pipe4, T, est pipe # testpipe. sh reads the file content through the input/output file name

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.