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
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
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 =
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
Sometimes, using "-" also means /dev/stdin. For example:
##
[root@xuexi ~]# cat /etc/fstab | cat -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.shIn 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 '' > ab.sh [root@xuexi ~]# truncate -s 0 ab.sh # truncate命令用于收缩和扩展文件大小 [root@xuexi ~]# dd if=/dev/null of=ab.sh
最后最重要的一点:在有重定向符号的语句中,命令执行之前已经将文件截断了。所以如果正在编辑一个文件并将编辑的结果重定向回这个文件将出现异常,因为截断后就没有合适的内容用于编辑。一个简单的示例如下:
[root@xuexi ~]# head a.log > a.log
有些时候直接使用">"覆盖输出是比较危险的。可以使用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
1.8.2.2 cat和重定向配合
配合cat使用可以分行输入内容到文件中。
[root@xuexi tmp]# cat <<eof>log.txt # 覆盖的方式输入到log.txt > this is stdin character > eof
也可以使用下面的方法。
[root@xuexi tmp]# cat >log1.txt <<eof > this is stdin character first! > eof
一方面,eof部分都必须使用"
[root@xuexi ~]# cat <<abcx > 123 > 345 > abcx 123 345
另一方面,>log1.txt表示将document的内容覆盖到log1.txt文件中,如果是要追加,则使用>>log1.txt。所以,追加的方式如下:
[root@xuexi tmp]# cat >>log1.txt <<eof > this is stdin character first! > eof
或
[root@xuexi tmp]# cat <<eof>>log1.txt > this is stdin character first! > eof
1.8.2.3 tee双重定向
可以使用tee双重定向。一般情况下,重定向要么将信息输入到文件中,要么输出到屏幕上,但是既想输出到屏幕又想输出到文件就比较麻烦。使用tee的双重定向功能可以实现该想法。如图。
tee [-a] file
选项说明:
-a:默认是将输出覆盖到文件中,使用该选项将变为追加行为。
file:除了输出到标准输出中,还将输出到file中。如果file为"-",则表示再输入一次到标准输出中。
例如下面的代码,将a开头的文件内容全部保存到b.log,同时把副本交给后面的的cat,使用这个cat又将内容保存到了x.log。其中"-"代表前面的stdin。
[root@xuexi tmp]# cat a* | tee b.log | cat - >x.log
还可以直接输出到屏幕:
[root@xuexi tmp]# cat a* | tee b.log | cat
tee默认会使用覆盖的方式保存到文件,可以使用-a选项来追加到文件。如:
[root@xuexi tmp]# cat a* | tee -a b.log | cat
现在就可以在使用cat和重定向创建文件或写入内容到文件的同时又可以在屏幕上显示一份。
[root@xuexi tmp]# cat <<eof | tee ttt.txt > x y > z 1 > eof x y z 1
1.8.2.4
在bash中,
here document在上文已经解释过了,对于here string,表示将
例如:
passwd --stdin user <<< password_value
等价于:
echo password_value | passwd --stdin user
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!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use