Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the shell pipe command pipe in linux

Shulou Source: shulou.com Published: 2022-06-01 07:14:21 09月23日 Update

This article mainly introduces the linux shell pipeline command pipe how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The pipeline command operator is "|", which can only handle the correct output information sent through the previous instruction, that is, the information of standard output, for stdandard

There is no direct processing capability for error information. Then, pass it to the next command as the standard input standard input.

Instructions for the use of pipe commands:

Let's take a look at the following picture:

The correct output of command1, as the input of command2, then the output of comand2, as the input of comand3, the output of comand3 will be displayed directly on the screen.

After passing through the pipe: the correct output of comand1,comand2 is not displayed on the screen.

Note:

1. The pipeline command only deals with the correct output of the previous command, not the error output.

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

Example:

The code is as follows:

[chengmo@centos5 shell] $cat test.sh | grep-n 'echo'

5: echo "very good!"

7: echo "good!"

9: echo "pass!"

11: echo "no pass!"

# read out the contents of the test.sh file and forward it to grep through the pipeline as input

[chengmo@centos5 shell] $cat test.sh test1.sh | grep-n 'echo'

Cat: test1.sh: there is no such 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, and the correct output is sent to grep via pipeline

[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 by test1.sh to the / dev/null file, and send the correct output to grep through the pipeline

[chengmo@centos5 shell] $cat test.sh | ls

Catfile httprequest.txt secure test testfdread.sh testpipe.sh testsh.sh testwhile2.sh

Envcron.txt python sh testcase.sh testfor2.sh testselect.sh test.txt text.txt

Env.txt release sms testcronenv.sh testfor.sh test.sh testwhile1.sh

# read test.sh content and send it to ls command through pipeline. Because ls does not support standard input, the data is discarded.

The example here is the verification of the above two points. The command that receives standard input can only be used as the right side of the pipe. Otherwise, the data will be discarded during the transfer process. Commonly used to receive data pipeline commands are: sed,awk,cut,head,top,less,more,wc,join,sort,split and so on, are text processing commands.

The difference between pipe commands and redirection

The difference is:

1. The command on the left should have standard output | the command on the right should accept standard input

The command on the left should have standard output > only files on the right

The command on the left should require standard input < the right can only be a file

2. The pipeline triggers two child processes to execute programs on both sides of the "|", while redirection is executed within one process.

These are summed up a lot online, in fact, as long as more clear usage, there must be a different description.

Example:

The code is as follows:

# can convert situations to each other

# 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!"

# "|" shell command must be on both sides of the pipe

[chengmo@centos5 shell] $grep-n 'echo'

Tags: Commands outputs pipes inputs files standards content right data processing instances results errors articles code information screens scripts operators operations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Shulou Tech Info MySQL Apple