Input/Output redirection, two-way redirection tee, and input/output redirection tee
In a simple sentence: "using input redirection can import the file to the command, while output redirection can write the data information originally to be output to the screen to the specified file"
After Linux is started, three file descriptors are opened by default (read and write files by assigning a number), which are: standard input 0, correctly output standard output 1, error output: error output 2
Data transmission during Linux Command Execution:
Standard intput: input from the keyboard by default. If it is 0, it indicates input from other files or commands.
Standard output: output to the screen by default. If it is set to 1, the output is to the file.
Standard error output: output to the screen by default. If the value is 2, the output is to the file.
Standard input (stdin): the code is 0, use <or < |
Standard output (stdout): the code is 1, use> or> |
Standard Error output (stderr): Code 2, Use 2> or 2> |
The/etc/crontab file exists by default, but no file/etc/crontab/vbird exists. Run the "cat/etc/crontab/vbird" command, cart will:
① Standard output: Read/etc/crontab and display the file content on the screen.
② Standard error output: the error message is displayed on the screen Because/etc/crontab/vbird cannot be found.
Redirect data streams
Standard output ("correct" output data) output/stored in the ist1_right file (via> ),
Standard Error output ("wrong" output information) output/stored in the list2_error file (through 2>)
The output redirection characters are as follows:
Symbol |
Function |
Command> File |
Redirect the standard output to a file (clear the data of the original file) |
Command 2> File |
Redirect error output to a file (clear the data of the original file) |
Command> File |
Redirects the standard output to a file (append to the end of the original content) |
Command 2> File |
Redirect error output to a file (append to the end of the original content) |
Command> file 2> & 1 or command> File |
Write the standard output and error output to the file (append to the original content) |
2> & 1 meaning:
<Http://blog.csdn.net/ggxiaobai/article/details/53507530>
<Https://www.cnblogs.com/zhenghongxin/p/7029173.html>
Error output redirection: Command 2> file (if the file does not exist, the system will automatically create it)
Standard output redirection: Command> File
The linuxprobe file actually exists, that is, the data can be written to the file using standard output, but the redirection of error output will not work, and the information will still be output to the screen
Write the standard output and error output to the file (the original file information is retained and appended to the file)
(&> Can be cleared before writing)
Ignore error messages directly (not displayed or stored): The black hole device/dev/null can "eat" any information directed to this device.
Append write: Command> File
For input redirection:
Symbol |
Function |
Command <File |
Use Files as standard input for commands |
Command <delimiter |
Read from standard input and stop until "Demarcation" is met (Inline input redirection) |
Command <file 1> file 2 |
Use File 1 as the standard command input and output the standard to file 2. |
|
|
Command <file: Use the/etc/passwd file as the standard input for the wc command, which is equivalent to the command "cat/etc/passwd | wc-l" to count the number of rows (count the number of users)
Replace the keyboard input with stdin to create a new file. the bashrc file is used as the standard input for cat commands and output to the created catfile file (similar to the cp command copy): Command <file 1> file 2
Command <delimiter: Use the cat command to directly output the input information to the catfile. The input is stopped only when the keyboard inputs the "eof" delimiter (ctrl + d ).
Bidirectional redirection: tee (T, T-pipe Interface) command
Tee sends data streams to files and screens at the same time.
The output to the screen is stdout, And the next command can be processed.
Parameters:
-A: add data to the file in the append mode (the tee command clears and overwrites by default)
References:
A good linux blog: