Standard input, output, and error
When we execute commands in the shell, each process is associated with three open files and uses file descriptors to refer to the files. Because the file descriptor is not easy to remember, the shell also gives the corresponding file name.
Here are the file descriptors and the filenames they typically correspond to:
File descriptor
Input file-Standard input 0: It is the input to the command, the default is the keyboard, or it can be the output of a file or other command.
Output file-standard Output 1: It is the output of the command, the default is the screen, or it can be a file.
Error output file-standard error 2: This is the output of the command error, the default is the screen, the same can be the file.
There are actually 1 2 file descriptors in the system, but as we can see in the table above, 0, 1, and 2 are standard inputs, outputs, and errors. You can use file descriptors 3 to 9 arbitrarily.
The standard input is the file descriptor 0. It is the input to the command, the default is the keyboard, or it can be the output of a file or other command.
The standard output is the file descriptor 1. It is the output of the command, the default is the screen, or it can be a file.
The standard error is file descriptor 2. This is the output of the command error, the default is the screen, also can be a file. You might ask, why do you have a special file specifically for errors? This is because many people prefer to save errors to a single file, especially when working with large data files, which can cause a lot of errors.
If you do not specifically specify a file specifier, the command will use the default file specifier (your screen, or, more specifically, your terminal).
File redirection
When you execute a command, you can specify standard input, output, and errors for the command, which you need to use for file redirection. The following table lists the most commonly used redirection combinations and gives the corresponding file descriptors.
You must use file descriptors when redirecting standard errors, but this is not required for standard input and output. For the sake of completeness, the following table lists two methods.
Command > FileName redirect the standard output to a new file
Command >> filename REDIRECT standard output to a file (append)
Command 1 > Fielname redirect the standard output to a file
Command > FileName 2>&1 redirect standard output and standard error to a file
Command 2 > FileName redirects the standard error to a file
Command 2 >> filename REDIRECT standard output to a file (append)
Command >> filename 2>&1 redirect standard output and standard error to a file (append)
Command < filename > filename2 the command command with the filename file as the standard input, with the filename2 file as the standard output
Command < filename Use the command command as a standard input to the filename file
Command << delimiter reads from the standard input until the delimiter delimiter is encountered
Command <&m the file descriptor m as the standard input
Command >&m redirect the standard output to the file descriptor m
Command <&-turn off the standard input
REDIRECT Standard output
Let's take a look at a standard output example. In the following command, the User ID field in the/etc/passwd file is arranged according to the user's life. The output of the command is redirected to the Sort.out file. Note that when using the sort command (or other commands that contain similar input file parameters), the redirect symbol must leave the sort command two spaces, otherwise the command will treat it as an input file.
$ Cat passwd | Awk-f: ' {print $} ' | Sort 1>sort.out
As can be seen from table 5-1, we can also use the following expression, the same result as above:
$ Cat passwd | Awk-f: ' {print $} ' | Sort >sort.out
You can append the output of many commands to the same file.
If you want to redirect the standard output to a file, you can use >filename. In the following example, all output of the LS command is redirected to the Ls.out file:
$ ls >ls.out
If you want to create an empty file with a length of 0, you can use >filename:
$ >myfile
REDIRECT Standard input
You can specify standard input for a command. This is the case in the awk chapter. An example of this is given below:
$ sort < Name.txt
In the above command, the input of the sort command is given in the form of redirection, but it is also possible to direct the corresponding
File as a parameter to the command:
$ sort Name.txt
In the example above, you can also further specify an output file, Name.out, by redirecting to the sort command.
This will not show any information on the screen (except for the error message):
$ sort <name.txt >name.out
When you send a message, you can use the Redirect method to send the contents of a file. In the following example, the user
Louise will receive a message containing the contents of the file Contents.txt:
$ Mail Louise < Contents.txt
The redirect operator Command << delimiter is a very useful order, often referred to as the "here" text block.
The shell will know that the input is finished by delimiter the delimiter until everything before the next same delimiter is entered as input and the next delimiter is encountered. This command is useful for automatic or remote routines. You can define the delimiter delimiter, the most common is EOF, and I like to use Mayday, which depends entirely on personal preferences. You can also enter variables after <<.
Here's an example where we created a file named myfile and used the term and logname variables.
$cat >> myfile <<mayday
> Hello there I am using a $TERIM terminal
> and my user name is $LOGNAME
> Bye ...
> MAYDAY
$ Cat MyFile
Hello there I am using a VT100 terminal
And my user name is Dave
Bye ...
REDIRECT Standard error
In order to redirect the standard error, you can specify the file descriptor 2.
The grep command did not find the file and output an error message to the terminal by default. Now let's redirect the error to the file/dev/null (which is actually the system's garbage bin):
$ grep "Trident" missiles 2>/dev/null
All the error outputs are sent to the/dev/null and no longer appear on the screen.
Merge standard output and standard errors
When merging standard output and standard errors, remember that the shell parses the corresponding command from left to right. An example is given below:
$ cleanup >cleanup.out 2>&1
In the example above, we redirect the output of the cleanup script to the >cleanup.out file, and its errors are redirected to the same file.
$ grep "Standard" * > Grep.out 2>&1
In the example above, the standard output and standard errors of the grep command are redirected to the Grep.out file. You may need to save all the output to a file when you use the "here" document mentioned earlier, so that you can be recorded if an error occurs. This can be done by using 2 > & 1
Http://blog.sina.com.cn/s/blog_51dc0fba0100lqgb.html
The shell can often be seen: >/dev/null 2>&1
Eg:sudo kill-9 ' ps-elf |grep-v grep|grep $1|awk ' {print $4} ' 1>/dev/null 2>/dev/null
The result of the command can be defined in the form of a%> output
/dev/null represents an empty device file
Where does the > delegate redirect to, for example: echo "123" >/home/123.txt
1 means stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null"
2 indicates stderr standard error
& means equivalent to, 2>&1, 2 output redirect equals 1
Then the statement in the title of this article:
1>/dev/null first indicates that the standard output is redirected to an empty device file, that is, not outputting any information to the terminal, which is plainly not displaying any information.
2>&1 then, the standard error output redirection is equivalent to the standard output because the standard error output is redirected to the empty device file because the standard output was previously redirected to an empty device file./////////////////////
Scripts use only standard input, standard output, and standard errors
The shell will automatically open and close the three file descriptors for us 0, 1, 2, and we don't need to open or close them explicitly. The standard input is the input of the command, the default point to the keyboard, the standard output is the output of the command, the default point to the screen, the standard error is the output of the command error message, the default point to the screen.
If you do not explicitly redirect, the command reads the input from the screen through the file descriptor 0, outputting the output and error information to the screen through file descriptors 1 and 2. But if we want to read input or produce output from other files (again, the I/O device is also a file in unix/linux), we need to redirect the 0, 1, and 2 uses. Its syntax is as follows:
Command < filename redirects the standard input to the filename file
Command 0< filename redirects the standard input to the filename file
Command > FileName redirect the standard output to the filename file (overwrite)
Command 1> fielname redirect the standard output to the filename file (overwrite)
Command >> filename redirects the standard output to the filename file (append)
Command 1>> filename redirects the standard output to the filename file (append)
Command 2> filename redirects the standard error to the filename file (overwrite)
Command 2>> filename redirects the standard output to the filename file (append)
Command > FileName 2>&1 redirect standard output and standard error to filename file (overwrite)
Command >> filename 2>&1 redirect standard output and standard error to filename file (append)
Command < filename >filename2 REDIRECT standard input to the filename file and redirect the standard output
To the Filename2 file
Command 0< filename 1> filename2 redirect The standard input to the filename file, redirecting the standard output
To the Filename2 file
The use of redirection has the following rules:
1) standard input 0, Output 1, error 2 need to be redirected separately, one redirect can only change one of them.
2) standard input 0 and standard output 1 can be omitted. (when it appears to the left of the redirect symbol)
3) The file descriptor is written directly to the left of the redirect symbol, and & is preceded by the right side.
4) There can be no space between the file descriptor and the redirect symbol!
Http://blog.sina.com.cn/s/blog_4aae007d010192qc.html
Linux standard input, output, and error and file redirection (RPM)