Detailed explanation of the pipe character of linux

The pipe symbol is mainly used for multiple command processing, that is, the output of the previous command is used as the standard input of the subsequent command; simply put, it is like the assembly line of a factory. After one process is completed, it is sent to the next process for processing...

Take a chestnut: sort the hello.sh file and find the line containing "better" after deduplication

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

  • view text
  • to sort
  • Deduplication
  • filter

 

 [1] The first process - view the text
First use the cat command to view the text, and the content printed on the screen is the output of the cat command

 [2] The second process - sorting
The result output by the previous cat command is thrown to the sort command through the pipeline, so the sort command is to sort the text output by the previous cat command

 [3] The third process - deduplication
sort and uniq can be used in combination to effectively deduplicate, so the text output after sort processing is thrown to uniq for processing through the pipeline, so uniq processes the sorted text, which can be effectively deduplicated Heavy

[4] The fourth process - filtering
The last step of filtering is also to filter the text output after the previous command, that is, the uniq command

The important point is here!
The important point is here!
The important point is here!

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

So what kind of commands support pipelines, and what kinds of commands don't 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

 

 When there is no parameter after sort, the output result of the previous command thrown to it by the pipe character is processed (that is, the standard output of the previous command is used as the standard input of this command)

 When the file to be deleted is not specified after rm, an error will be reported and the parameter is lost. Therefore, commands such as rm do not support reading parameters from standard input, but only support specifying parameters on the command line, that is, specifying the file to be deleted;

Standard input and command line parameters which take precedence?

 

It can be seen that when the command line parameter of sort (here b.txt) is not empty, sort will not read the parameters in the standard input, but read the command line parameters

"-" means standard input , that is, the output of the command cat aaa.txt, which is equivalent to sorting the file bbb.txt and standard input together, which is equivalent to sort aaa.txt bbb.txt

 

Thinking: For commands such as rm and kill, when we write scripts, we often encounter the need to query the process number of a certain process and then kill the process, find a certain file and then delete it. What should we do? Then use xargs!

Guess you like

Origin blog.csdn.net/bleauchat/article/details/126395920