Linux entry record: 16. Multi-command collaboration in Linux: pipelines and redirection, linux redirection
I. Multi-command collaboration
In Linux, most commands are very simple, and there are few complex functional commands. Each command often only implements one or more simple functions. By combining commands of different functions, You can implement a complex function.
In Linux, the data returned by almost all commands is plain text (because each command runs under CLI), and the data in plain text form is the input format of most commands, this makes multi-command collaboration possible.
In Linux, the command line provides pipelines and redirection mechanisms. Multi-command collaboration is achieved through pipelines and redirection.
Ii. Standard stream
A terminal may need to process various types of data, such as int, float, double, char, string, or even a text file, terminals pay more attention to the content of data, ignore the data types and focus on their content, and abstract them into a "Stream" concept. Data streams are generally called I/O streams.
Almost every terminal has data stream I/O (input and output). Generally, input stream, output stream, error stream, and file stream are used based on the stream trend and purpose, generally, there are three default standard streams:
Name |
Description |
No. (File descriptor) |
Default Device |
STDIN |
Standard input stream |
0 |
Keyboard |
STDOUT |
Standard output stream |
1 |
Terminal |
STDERR |
Standard Error Liu |
2 |
Terminal |
STDIN indicates the standard input stream. The default value is keyboard, and the file descriptor is 0. STDOUT and STDERR indicate the standard output stream and the standard error stream respectively. Generally, they are terminals by default, and the file descriptors are 1 and 2 respectively.
3. Pipelines and redirection 1. Redirection
Redirection can redirect I/O streams (associated) to files, rather than the default terminal. Redirection is usually used to save the output or error information of a command to a specified file.
Common operators include the following:
> Redirecting STDOUT to a file (overwrite)> redirecting STDOUT to a file (append) 2> redirecting STDERR to a file (overwrite) 2> & 1 redirect STDERR and STDOUT to a file (overwrite) <redirect STDIN to a file
Example:
Echo Hello, Linux> echo. md prints a statement and outputs it to the echo cnblogs.com> echo. md prints a statement and appends it to the file ls noexist 2> ls. md lists directories and outputs error messages to the file ls noexist 1>. md 2> & 1 List directories, output to files, and merge error messages to standard output (also output to files) cat> output. md <input. md input. md file as standard input, output. md as standard output
2. MPS queue
Pipeline operations can use the output of one command as the input of another command. Pipelines are usually used to combine different commands to implement a complex function.
OPERATOR:
| Use the STDOUT of one command as the STDIN of another command
Example:
Ls-ld. | cat> ls. md lists the directory information and outputs it to the file.
Reference link:
Linux Input/Output Error Redirection
Detailed analysis of linux shell data redirection (input redirection and output redirection)