Standard input, standard output and standard errors
The input of a program can come from a keyboard, or from a file or other device; similarly, a program can display the output on the screen or save it to a file. This involves standard input, standard output and standard errors.
The program's input is standard input, and the default is the keyboard, which users can specify as a file or other device.
There are two types of outputs of a program, namely standard output and standard error, where standard output is the normal output of the program and standard error is the error output of the program. Both are specified as screens by default, and users can specify them as files or other devices.
The following program reads input from the keyboard and displays the results on the screen, that is, standard input, standard output and standard errors are all set by default:
$ ls ~ homework name
Redirect
Users can redirect inputs and outputs, i.e. specify standard inputs, standard outputs, and standard errors.
Standard input is specified using <. The following command takes the input file as input to the sort command, that is, redirects the standard input of the sort command to the input file:
$ cat input zhao qian sun li zhou wu zheng wang $ sort < input li qian sun wang wu zhao zheng zhou
In this example, the input file is used as standard input to the sort command.
Use > Redirect stdout:
$ ls ~ > output $ cat output homework input name output
As shown above, the standard output of ls ~ is redirected to output, so its output is written to the output file.
It should be noted that if the output does not exist, the output file will be automatically created; if the output exists, the content of the output will be cleared and new content will be written.
If you just want to append the end of the existing content in the output file, you can use >>:
$ date >> output $ cat output homework input name output Sat Jun 30 18:18:53 CST 2018
The following command redirects the standard input to input and the standard output to output:
$ sort < input > output # Overwrite output content$ sort < input >> output # existoutputAdd content at the end
File descriptor
Each input source and output site has a descriptor, the standard input descriptor is 0, the standard output descriptor is 1, and the standard error descriptor is 2.
Use 0< to redirect standard input. In fact, since the program's default redirect input is standard input, the following two commands are equivalent:
$ sort < input $ sort 0< input
Similarly, the program's default redirection output is standard output, so the following two commands are also equivalent:
$ ls > output $ ls 1> output
The following command redirects the standard input to the input file, the standard output to the output file (append), and the standard error to the error file:
$ sort < input 1>> output 2> error
Combining standard output and standard errors
The following command redirects the standard output to an output file, and the standard error is still displayed on the screen:
$ sort < input > output
The following command redirects the standard output to an output file and the standard error to an error file:
$ sort < input > output 2> error
The following command redirects the standard output to the output file, and redirects the standard error to the standard output using 2>&1:
$ sort < input > output 2>&1
In the example above, both standard output and standard errors are redirected to output files.
Notice,The following commands will cause mutual overwrites of standard output and standard errors and are therefore not available:
$ sort < input > output 2> output
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.