Home Backend Development Python Tutorial Basic Tutorial on Shell Pipe Redirection

Basic Tutorial on Shell Pipe Redirection

Aug 15, 2017 pm 03:54 PM
shell Tutorial Redirect

Pipeline exists to solve the problem of inter-process communication. It allows data to be transferred between two processes, and the output data of one process is transferred to another process as its input data

1.8.1 Anonymous Pipe"|"

The pipe symbol means as its name suggests. It passes the data from the pipe inlet to the pipe outlet through the pipe, just like a pipe.

Pipeline exists to solve the problem of inter-process communication. It allows data to be transferred between two processes, and the output data of one process is transferred to another process as its input data. The left side of the pipe is the data giver, and the right side of the pipe is the data receiver.

For exampleecho "abcd" | passwd --stdin username means that the output result "abcd" of the process echo is used as the input data of the process passwd.

Basic pipe symbols and their usage are easy to understand. The question now is, for ps aux | grep "ssh" , why does the grep process appear in the results?


[root@xuexi ~]# ps aux | grep ssh
root    1211 0.0 0.1 82544 3600 ?    Ss  Jul26  0:00 /usr/sbin/sshd -D
root   25236 0.0 0.2 145552 5524 ?    Ss  05:28  0:00 sshd: root@pts/0
root   25720 0.1 0.2 145416 5524 ?    Ss  06:15  0:00 sshd: root@pts/1
root   25770 0.0 0.0 112648  948 pts/1  S+  06:15  0:00 grep --color=auto ssh
Copy after login

According to the general idea, ps is executed first, and after getting the output, the output data is passed to grep. At this time, grep has not run but ps has finished running. Why? Can we collect statistics about the grep process? The reason is that the pipeline implements inter-process communication, and there is overlap between the two processes. After running the ps process, the process information starts to be collected. grep has also started and is in the state of waiting to receive data. When ps collects any data, the output will be released. The data is passed into memory and piped to grep for filtering.

The essence of a pipe is data transfer. The output data on the left side of the pipe is put into memory and read by the process on the right side of the pipe. If the memory is not enough to completely store the output data, the process on the left side of the pipe will wait until the right side of the pipe takes out part of the data in the memory to allow the process on the left side of the pipe to continue output, and the process on the right side of the pipe will start immediately after the process on the left side of the pipe starts. Started, but it has been in a waiting state, waiting to receive data passed by the pipe.

In other words, the processes on the left and right sides of the pipe run in almost no order.

So how does ps aux | grep "ssh" prevent grep's own process from appearing in the results? There are two methods:

Method one: ps aux | grep "ssh" | grep -v "grep"

Method two: ps aux | grep "ss[h ]"


[root@xuexi ~]# ps aux | grep ss[h]
root    1211 0.0 0.1 82544 3600 ?    Ss  Jul26  0:00 /usr/sbin/sshd -D
root   25236 0.0 0.2 145552 5524 ?    Ss  05:28  0:00 sshd: root@pts/0
root   25720 0.0 0.2 145416 5524 ?    Ss  06:15  0:00 sshd: root@pts/1
Copy after login

Method one is to use the "-v" feature of grep, and method two is to use the feature of regular expressions.

In the process of using anonymous pipes, you may have discovered that the processes on both sides of the pipe belong to the same process group, which means that the data on the left side of the pipe can only be passed to the process on the right side of the pipe. No process can read this data. But in addition to anonymous pipes, there are also named pipes. A named pipe stores the data of a process in a pipe file (fifo). Other processes can read the pipe file to read the data in it, which means that the data is no longer restricted. Reader. Regarding named pipes, please refer to Linux/unix operating system kernel or programming books, which will generally provide detailed introductions.

1.8.2 Redirection

1.8.2.1 Redirect basics

The most common The file descriptors of standard input (stdin), standard output (stdout) and standard error output (stderr) are 0, 1 and 2 respectively, where 0, 1 and 2 can also be considered as their numerical codes. The output information can be thought of as the information printed on the screen, and the one that does not give an error is the standard output, and the one that gives an error prompt is the standard error output. Of course, this explanation is biased, but it is easy to understand. You can also customize your own descriptors to implement advanced redirection. Their usage may be introduced in future articles.

Standard input = /dev/stdin = code 0 = < or << symbol.

Standard output = /dev/stdout = code 1 = > or >> symbol.

Standard error output = /dev/stderr = code 2 = use 2> or 2>> notation.

<, >, 2> implement the overwriting function, >>, 2>> implement the additional function, but << is not an additional function, but means here Generate a document (here document), which is explained later in the content of the cooperation between cat and redirection. In addition, there is <<<, which means a here string, see also below.

Sometimes, using "-" also means /dev/stdin. For example:


##

[root@xuexi ~]# cat /etc/fstab | cat -
Copy after login

The symbols 2>&1 and &> are common in scripts. They all mean to redirect both stdout and stderr to the same place, that is, redirect all Output content. Such as the most common "&> /dev/null".

Throwing stdout or stderr to /dev/null means discarding the output information. Conversely, redirecting /dev/null to a file means clearing the file.


[root@xuexi ~]# cat /dev/null > ab.sh
Copy after login

In addition, there are several ways to quickly clear files


[root@xuexi ~]# > ab.sh
[root@xuexi ~]# : > ab.sh       # 或"true >ab.sh",其实它们都等价于">ab.sh"
[root@xuexi ~]# echo &#39;&#39; > ab.sh
[root@xuexi ~]# truncate -s 0 ab.sh  # truncate命令用于收缩和扩展文件大小
[root@xuexi ~]# dd if=/dev/null of=ab.sh
Copy after login

最后最重要的一点:在有重定向符号的语句中,命令执行之前已经将文件截断了。所以如果正在编辑一个文件并将编辑的结果重定向回这个文件将出现异常,因为截断后就没有合适的内容用于编辑。一个简单的示例如下:


[root@xuexi ~]# head a.log > a.log
Copy after login

有些时候直接使用">"覆盖输出是比较危险的。可以使用set -C来设置如果输出重定向文件已经存在则不覆盖。使用set +C来取消set -C的效果。如果在设置了set -C时仍然想强制覆盖,可以使用“>|”代替“>”来重定向输出。同理错误输出也有此特性。


[root@xuexi tmp]# set -C
[root@xuexi tmp]# cat flip >ttt.txt
-bash: ttt.txt: cannot overwrite existing file
[root@xuexi tmp]# cat flip >| ttt.txt
[root@xuexi tmp]# set +C
Copy after login

1.8.2.2 cat和重定向配合

配合cat使用可以分行输入内容到文件中。


[root@xuexi tmp]# cat <<eof>log.txt  # 覆盖的方式输入到log.txt
> this is stdin character
> eof
Copy after login

也可以使用下面的方法。


[root@xuexi tmp]# cat >log1.txt <<eof 
> this is stdin character first!
> eof
Copy after login

一方面,eof部分都必须使用"<


[root@xuexi ~]# cat <<abcx
> 123
> 345
> abcx
123
345
Copy after login

另一方面,>log1.txt表示将document的内容覆盖到log1.txt文件中,如果是要追加,则使用>>log1.txt。所以,追加的方式如下:


[root@xuexi tmp]# cat >>log1.txt <<eof 
> this is stdin character first!
> eof
Copy after login


[root@xuexi tmp]# cat <<eof>>log1.txt 
> this is stdin character first!
> eof
Copy after login

1.8.2.3 tee双重定向

可以使用tee双重定向。一般情况下,重定向要么将信息输入到文件中,要么输出到屏幕上,但是既想输出到屏幕又想输出到文件就比较麻烦。使用tee的双重定向功能可以实现该想法。如图。


tee [-a] file
Copy after login

选项说明:

-a:默认是将输出覆盖到文件中,使用该选项将变为追加行为。

file:除了输出到标准输出中,还将输出到file中。如果file为"-",则表示再输入一次到标准输出中。

例如下面的代码,将a开头的文件内容全部保存到b.log,同时把副本交给后面的的cat,使用这个cat又将内容保存到了x.log。其中"-"代表前面的stdin。


[root@xuexi tmp]# cat a* | tee b.log | cat - >x.log
Copy after login

还可以直接输出到屏幕:


[root@xuexi tmp]# cat a* | tee b.log | cat
Copy after login

tee默认会使用覆盖的方式保存到文件,可以使用-a选项来追加到文件。如:


[root@xuexi tmp]# cat a* | tee -a b.log | cat
Copy after login

现在就可以在使用cat和重定向创建文件或写入内容到文件的同时又可以在屏幕上显示一份。


[root@xuexi tmp]# cat <<eof | tee ttt.txt
> x y
> z 1
> eof
x y
z 1
Copy after login

1.8.2.4 <<和<<<

在bash中,<<和<<<是特殊重定向符号。<<表示的是here document,<<<表示的是here string。

here document在上文已经解释过了,对于here string,表示将<<<后的字符串作为输入数据。

例如:


passwd --stdin user <<< password_value
Copy after login

等价于:


echo password_value | passwd --stdin user
Copy after login

The above is the detailed content of Basic Tutorial on Shell Pipe Redirection. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

How to quickly delete the line at the end of a file in Linux How to quickly delete the line at the end of a file in Linux Mar 01, 2024 pm 09:36 PM

When processing files under Linux systems, it is sometimes necessary to delete lines at the end of the file. This operation is very common in practical applications and can be achieved through some simple commands. This article will introduce the steps to quickly delete the line at the end of the file in Linux system, and provide specific code examples. Step 1: Check the last line of the file. Before performing the deletion operation, you first need to confirm which line is the last line of the file. You can use the tail command to view the last line of the file. The specific command is as follows: tail-n1filena

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

DisplayX (monitor testing software) tutorial DisplayX (monitor testing software) tutorial Mar 04, 2024 pm 04:00 PM

Testing a monitor when buying it is an essential part to avoid buying a damaged one. Today I will teach you how to use software to test the monitor. Method step 1. First, search and download the DisplayX software on this website, install it and open it, and you will see many detection methods provided to users. 2. The user clicks on the regular complete test. The first step is to test the brightness of the display. The user adjusts the display so that the boxes can be seen clearly. 3. Then click the mouse to enter the next link. If the monitor can distinguish each black and white area, it means the monitor is still good. 4. Click the left mouse button again, and you will see the grayscale test of the monitor. The smoother the color transition, the better the monitor. 5. In addition, in the displayx software we

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Mar 22, 2024 pm 12:21 PM

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

See all articles