8.3.3 output redirectionOutput redirection refers to redirecting standard output or standard error output of commands or executable programs to a specified file. The command output is not displayed on the screen, but written to the specified file for future troubleshooting or other purposes. Output redirection is more commonly used than input redirection, which can be used in many cases. For example, if a command has a lot of output and cannot be fully displayed on the screen, redirect the output to a file and open the file with a text editor to view the output information, you can use this method to save the output of a command. Also, output redirection can be used to input a command as another command. Another simpler method is to use pipelines, which will be described in the following section.
The general format of output redirection is: Command> file name, that is, the output redirection symbol is ">", as shown in Example 8-11.
[Example 8-11]
# Redirecting output to a file
[Root @ CentOS ~] # Ls-l/> dir.txt
[Root @ CentOS ~] # Head-n5 dir.txt
Total 114
Dr-xr-xr-x.2 root root4096 Jun8 00:54 bin
Dr-xr-xr-x.5 root root1024 Apr 13 boot
Dr-xr-xr-x.7 root root4096 Mar6 cdrom
Drwxr-xr-x.18 root root4096 Jun8 0:07 data
Use the "ls-lonics command to display the current directory and file, and output the result to the dir.txt file in the current directory instead of on the screen. You can use the cat command for the content of the nvidir.txt file. note whether the result is the same as that displayed when you directly use the "ls-l" command.
Note: If the file after the ">" symbol already exists, the file will be overwritten.
To avoid overwriting the specified file content in the output redirection command, Shell provides an append method for output redirection. The output append redirection function is similar to the output redirection function. The difference is that the output result of the command or executable program is appended to the end of the specified file, in this case, the original content of the file is not overwritten. Append the redirection operator ">" in the format of command> file name, as shown in Example 8-12.
[Example 8-12]
# Use redirection to append file content
[Root @ CentOS ~] # Ls-l/usr> dir.txt
The output of the above command will be appended to the last position of the file, and the original content will not be overwritten.
8.3.4 error output redirectionLike the standard output redirection of a program, the program's error output can also be reoriented. You can use the symbol "2>" or append the symbol "2>" to redirect error output. To print any program error information to a file for troubleshooting, use the method in Example 8-13.
[Example 8-13]
# If the file does not exist, a standard error is generated, which is usually displayed on the screen.
[Root @ CentOS ~] # Ls/xxxx
Ls: cannot access/xxxx: No such file or directory
#1 indicates the redirection standard output, but is it not an error output? the output is still printed to the screen.
[Root @ CentOS ~] # Ls/xxxx 1> stdout
Ls: cannot access/xxxx: No such file or directory
# REDIRECT standard output and standard error output respectively
[Root @ CentOS ~] # Ls/xxxx 1> stdout 2> stderr
# View the file content, consistent with the result printed to the screen
[Root @ CentOS ~] # Cat stderr
Ls: cannot access/xxxx: No such file or directory
# Direct both standard output and standard error output to standard output files
[Root @ CentOS ~] # Ls/xxxx 1> stdout 2> & 1
[Root @ CentOS ~] # Cat stdout
Ls: cannot access/xxxx: No such file or directory
# Another redirection syntax
[Root @ CentOS ~] # Ls/xxxxx &> stderr
[Root @ CentOS ~] # Ls/xxxxx/&> stdout
# View output file content
[Root @ CentOS ~] # Head stdout
Ls: cannot access/xxxxx: No such file or directory
/:
Bin
Boot
Cdrom
The/xxxx directory does not exist, so there is no standard output, only error output. The preceding example demonstrates the error Output Content. when the standard output is redirected, the standard output is not redirected, so the error output is printed to the screen. Use "2> stderr" to locate the error output to the specified file, and redirect the standard error output to the standard output. after the output is executed, no content is displayed on the screen, run the cat command to view the file content and see the error message of the above command. You can also use another output redirection operator "&>" to send standard output and error output to the same file. Table 8-3 lists common input/output redirection methods.
Table 8.3 common redirection definitions
Parameters |
Description |
Command> filename |
Redirects standard output to a file |
Command> filename |
Redirects the standard output to a file. |
Command 1> fielname |
Redirects standard output to a file |
Command> filename 2> & 1 |
Redirects the standard output and standard error output to a file. |
Command 2> filename |
Redirects the standard error output to a file. |
Command <filename> filename2 |
Input with filename as the standard and output with filename2 as the standard |
Command <filename |
Use filename as the standard command input. |
Command <delimiter |
Reads data from standard input until delimiter is encountered. |
8.3.5 MPs queueThere are two ways to use the output of one program or command as the input of another program or command: one is to combine two commands or programs through a temporary file; another method is to use pipelines.
The pipeline can connect a series of commands. the output of the preceding command can be used as the input of subsequent commands. the output of 1st commands can be sent to 2nd commands using the pipeline, the output of 2nd commands is used as the input of 3rd commands, and so on. If no output redirection is used in the command line, the output of the last command or other command execution exceptions in the pipeline line are displayed on the screen. Use the pipe character "|" to create a pipe line, as shown in Example 8-14.
[Example 8-14]
[Root @ CentOS ~] # Cat/etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADD
IPADDR = 192.168.3.100
# Post-pipeline
[Root @ CentOS ~] # Cat/etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADD | awk-F = '{print $2 }'
192.168.3.100
The content output by the cat command in the preceding example is sent to the grep command in the form of a pipe, and then the file content is searched through string matching.