Solve the problem that the pipeline is closed by the receiver in Linux.

Source: Internet
Author: User
Article Title: solve the problem that the pipeline is closed by the recipient in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Pipeline in Linux shell | is a very convenient function. You can use the output of one program as the input of another program. In this way, we can combine multiple commands, saves the hassle of temporary files. Similar usage is also available in windows, such as dir | more. Anyone who has learned the doscommand should know it.

Since it is a pipeline, There is an entrance and an exit, each corresponding to an application. Normally, the output of the entry application should be accepted by all the export applications, however, in some special cases, the exported application closes the pipeline in advance. For example, when querying the svn Update log, only the previous line is used:

$ Svn log | head

----------------

Rwolf | Fwolf | 13:38:47 + 0800 (Mon, 28 May 2007) | 4 lines

Update records.

Svn: Write error: Broken pipe

Because the head only needs to use the first 10 rows of input (the default number of rows can also be specified by the user), it is unnecessary to receive the remaining output, and the pipeline is closed in advance, after the application svn at the MPs queue entry is found, an error is reported and the application exits. In this example, the error message is very clear, but not all applications are like this, for example:

$ Find.-name "* rc" | xargs-I cat {} | head-1

[Desktop]

Xargs: cat: terminated by signal 13

The error message does not seem easy to understand. In fact, it means that xargs finds that its sub-process cat has been aborted due to signal 13. Because xargs itself is a loop operation, it stops the loop after an error is found. Signal 13 is when cat tries to write data to a closed pipe pipeline, generated by the system. After receiving the cat, it stops. Similar to the effect of pressing ctrl + c in the cat output process.

How can this problem be avoided? It is easy to use a program that does not close the MPs queue in advance, especially when used with xargs. For example, you can use head as follows:

$ Cat file | head-1

Although cat is still disabled by signal 13, bash does not report an error. Therefore, only one file can be operated. Even if Wildcards are used, head can only be directed to the first file. If you want to add file traversal, you can use:

$ For file in. * rc; do cat $ file | head-1; done

Cat will still be disabled, but for will ignore it and continue the loop. Head can also directly specify the file name, so that we can discard cat:

$ Find.-name "* rc" | xargs-I head-n1 {}

I personally think this is the perfect solution, that is, you can use the find powerful search command, and it does not involve pipeline issues. However, if the file name has no special requirements, there is a simpler method:

$ Head-n1. * rc

Specify a file with a wildcard in the header parameter.

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.