Usually when executing commands in the Shell, we see the result of the execution below the input command, and the operating system defaults to outputting the result of the command to the monitor. Of course, we can also specify the output path manually, or enter the path, which is I/O redirection.
1. Standard output redirection
With the cat command, the execution results of the command will be printed on the screen.
We use > for output redirection, when the command execution results are no longer printed on the screen, but the execution results are saved to the./target.xxx file.
Take a look at the contents of the./target.xxx file:
2. Standard input redirection
The TR command can read the standard input from the keyboard and then print out the execution results of the command:
The cursor below indicates that the TR command waits for the user's input and then prints them on the screen. CTRL + C can terminate this command execution. By default, TR reads the content output from the keyboard, and if we want to read the content in other files, we use the standard input redirect.
3. >> and <<
When using > for output redirection, the last output overwrites the previous output, and if we do not want to overwrite it, use >> for the append redirect.
Unlike >>, << does not imply an append redirect, but rather a document input, or multiple lines of input. Use the << when you need to use the delimiter flag, about this command, students who have learned PHP should have a better understanding. Delimiters can be arbitrarily named, as long as the two ends remain consistent.
Input redirection and output redirection can also be connected with use, where << and >> examples are used:
4. Standard error output stream (STDERR)
In addition to the marked input stream (STDIN), the standard output stream (STDOUT), there is also a standard error output stream (STDERR). The standard error output stream and the standard output stream are different data streams.
If an error occurs in the command, the error message is printed on the screen, and because STDERR and STDOUT belong to different streams, the error message is not exported to the./result.
The standard error output stream is redirected using 2> (overwrite) or 2>> (append):
No error message is printed on the screen and the error message is output to the./result file.
If we want to output the results of the command execution (normal results and error messages) to a file, you can use the >> and 2>> connections, or you can use &> or &>> redirect STDOUT and STDERR to In the same file:
5. Piping
Pipelines represent the flow of data: When data is processed somewhere, it is piped to another place for processing. Use in Linux | As a pipe connector, represents the result of the last command's execution as input to the next command. Here are the two instance usages of the pipeline:
6.set Command
Set is a built-in command in bash that can be turned on or off for some functions. For security reasons, we generally do not allow overwrite redirection, which is a set-c that can be used to suppress overrides.
Use the Set +C command to indicate that override redirection is allowed.
Do we still want to do this if we use Set-c to prohibit overwriting redirects? We can use COMMAND >| FILE, you can do this by adding a vertical line to the >.
7. Summary
This article describes the implementation of I/O redirection in Linux, including standard input, output redirection, and standard error output redirection, as well as the difference between overwrite redirection and append redirection. Finally, we introduce the pipeline in Linux, we use the pipeline to connect the command, the output of the previous command as the input of the latter command. The content is relatively simple, as a separate point of knowledge, or it is independent of the written, convenient for later inspection. The following article describes the grep command and the regular expression, and after learning to use the grep command, we will be very quick and easy to process files on Linux.
Charleylla Reprint Please specify the Source: http://www.cnblogs.com/charleylla/p/5988213.html
[Linux 005]--io redirection