What is the function of the linux pipe character

05-18-2023

The editor in this article introduces the function of the linux pipe symbol in detail. The content is detailed, the steps are clear, and the details are handled properly. I hope this article on what is the function of the linux pipe symbol can help you solve your doubts. Slowly deepen your thinking, let's learn new knowledge together.

In linux, the pipe symbol | is used to connect multiple instructions, the output stream of the previous instruction will be used as the operation object of the next instruction, and its command format Instruction 1 | Instruction 2 | ..., the last instruction of this command must be able to receive standard input stream commands before it can be executed. The pipe character is mainly used for multiple command processing, and the print results of the previous commands are used as the input of the subsequent commands.

Pipeline is a very important communication method in Linux, which directly connects the output of one program to the input of another program. The pipes that are often said mostly refer to unnamed pipes. Unnamed pipes can only be used between processes with kinship relationships. This is the biggest difference between them and named pipes.

The well-known pipe is called named pipe or FIFO (first in first out), which can be created with the function mkfifo().

The pipe symbol in linux

| The function of the pipe symbol is to connect multiple instructions, the output of the previous instruction The stream will be the operation object of the next instruction, and its command format is instruction 1 | instruction 2 | .... The next instruction of this command must be able to receive the standard input stream command before it can be executed.

The operator of the pipeline command is: |, it can only process the correct output information sent by the previous command, and has no ability to directly process error information. Then, it is passed to the next instruction as an operation object.

Syntax:

Command 1 | Command 2 | ...

The pipe character is mainly used for multiple command processing, the printing of the previous command The result is used as input for subsequent commands. To put it simply, it is just like the assembly line of a factory. After one process is completed, it is sent to the next process for processing...

To give a chestnut: after sorting the hello.sh file and deduplicating it Find the line containing "better"

The command is: cat hello.sh | sort | uniq | grep 'better'

    < li>

    View Text

  • Sort

  • Remove Duplication

  • < p>filter

【1】The first process - view the text

First use the cat command to view the text and print it to the screen The content is the output of the cat command

[root@linuxforliuhj test]# cat hello.sh hello this is linux be better be better i am lhj hello this is linux i am lhj i am lhj be better i am lhj have a nice day have a nice day hello this is linux hello this is linux have a nice day zzzzzzzzzzzzzzz dddddddd gggggggggggggggggggg[root@linuxforliuhj test]#

【2】Second process - sorting

Throw the output result of the previous cat command to the sort command through the pipeline, so the sort command is Sort the text output by the previous cat command

[root@linuxforliuhj test]# cat hello.sh | sortbe better be better be better dddddddd gggggggggggggggggggg have a nice day have a nice day have a nice day hello this is linux hello this is linux hello this is linux hello this is linux i am lhj i am lhj i am lhj i am lhj zzzzzzzzzzzzzzzz[root@linuxforliuhj test]#

【3】The third process - deduplication

As mentioned in the previous article introducing uniq, the combination of sort and uniq can effectively remove Heavy, so the sorted output text is thrown to uniq through the pipeline, so uniq processes the sorted text, which can be effectively deduplicated

[root@linuxforliuhj test]# cat hello.sh | sort | uniqbe better dddddddd gggggggggggggggggggg have a nice day hello this is linux i am lhj zzzzzzzzzzzzzzzz[root@linuxforliuhj test]#

【4】The fourth process - filtering

The last step of filtering is also to process the text output by the previous command, that is, the uniq command Filter

[root@linuxforliuhj test]# cat hello.sh | sort | uniq | grep 'better'be better[root@linuxforliuhj test]#

Here comes the key !

Here comes the key point!

Here comes the key point!

The above cat, sort, uniq, grep and other commands all support the pipe character, because these commands can read the text to be processed from the standard input (that is, read the parameters from the standard input); and for some commands, such as Commands such as rm and kill do not support reading parameters from standard input, but only support reading parameters from the command line (that is, the file or directory to be deleted must be specified after the rm command, and the process number to be killed must be specified after the kill command, etc. )

What kind of commands support pipelines, and what kinds of commands do not support pipelines?
In general, commands that process text, such as sort, uniq, grep, awk, sed, etc., all support pipelines; commands that do not process text, such as rm and ls, do not support pipelines

< /blockquote>

[root@linuxforliuhj test]# cat hello.sh | sortbe better be better be better dddddddd gggggggggggggggggggg have a nice day have a nice day have a nice day hello this is linux hello this is linux hello this is linux hello this is linux i am lhj i am lhj i am lhj i am lhj zzzzzzzzzzzzzzzz[root@linuxforliuhj test]#

When there is no parameter after sort, then process the output of the previous command that the pipe character throws to it (that is, the standard output of the previous command is used as the standard input of this command)

[root@linuxforliuhj test]# lsbeifen.txt hello.sh mk read.ln read.sh read.txt sub.sh[root@linuxforliuhj test]# ls | grep read.shread.sh[root@linuxforliuhj test]# ls | grep read.sh | rmrm: missing operand Try 'rm --help' for more information.[root@linuxforliuhj test]#

When the file to be deleted is not specified after rm, an error will be reported for missing parameters. Therefore, commands such as rm do not support standard Input read parameters, only supports specifying parameters on the command line, that is, specifying the file to be deleted.

Standard input and command line parameters, which one takes precedence?

There are the following two files

[root@linuxforliuhj test]# cat a.txt aaaa dddd cccc bbbb[root@linuxforliuhj test]# cat b.txt 1111333344442222[root@linuxforliuhj test]#

Execute command: cat a.txt | sort

[root@linuxforliuhj test]# cat a.txt | sortaaaa bbbb cccc dddd[root@linuxforliuhj test]#

When the command line parameter of sort is empty, the output of the previous command is used as the input of this command by default

Execute the command: cat a.txt | sort b.txt

[root@linuxforliuhj test]# cat a.txt | sort b.txt 1111222233334444[root@linuxforliuhj test]#

You can see, When the command line parameters of sort (here b.txt) are not empty, sort will not read the parameters in the standard input, but read the command line parameters

Execute the command: cat a. txt | sort b.txt -

[root@linuxforliuhj test]# cat a.txt | sort b.txt -1111222233334444aaaa bbbb cccc dddd[root@linuxforliuhj test]#

" - "Indicates the standard input, that is, the output of the command cat a.txt, which is equivalent to sorting the file b.txt and the standard input together, which is equivalent to sort a. txt b.txt

[root@linuxforliuhj test]# sort a.txt b.txt1111222233334444aaaa bbbb cccc dddd[root@linuxforliuhj test]#

After reading this, what is the function of the linux pipe symbol has been introduced. If you want to master the knowledge points of this article, you need to practice and use it yourself If you want to know more articles about related content, please pay attention to Yisu Cloud Industry Information Channel.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us