Standard input/output and redirection, Linux distribution Fedora Core Linux, and Red Hat's original Red Hat Linux development team will continue to participate in the development of this release.
standard input and output
We know that executing a shell command line usually automatically opens three standard files, standard input files (stdin), usually the keyboard for the terminal, standard output files (stdout), and standard error output files (stderr), which correspond to the screen of the terminal. The process will get the input data from the standard input file, output the normal output data to the standard output file, and send the error message to the standard error file.
We take the cat command as an example, the function of the Cat command is to read the data from the file given in the shell command line and send the data directly to the standard output. If you use the following command:
$ cat config will display the contents of the file config to the screen in turn. However, if there is no parameter in the cat's shell command line, it reads the data from the standard input and sends it to the standard output. For example:
$ cat
Hello World
Hello World
Bye
Bye
Each line entered by the user is immediately exported to the screen by the cat command. Another example, command sort reads the body of the file in rows (when no file name is given in the shell command line, reads from standard input), sorts it, and sends the result to standard output. The following example reads a purchase order from the standard input and sorts it.
$ sort
Bananas
Carrots
Apples
Bananas
Carrots
Then we got the sorted purchase order on the screen. The direct use of standard input/output files has the following problems: input data from the terminal input, the user spent half a day to enter the data can only be used once. The next time you want to use this data, you have to re-enter it. And in the terminal input, if the input error modification is not very convenient. The information on the output to the terminal screen can only be seen and not moved. We cannot do much more with this output, such as further processing of the output as input to another command. To solve the above problem, the Linux system introduces two other mechanisms for input and output transmission, namely, input/output redirection and pipelines.
Input Redirection
Input redirection refers to redirecting the standard input of a command (or executable program) to a specified file. That is, the input can come from a specified file, not from the keyboard. So, input redirection is primarily used to change the input source of a command, especially those that require a lot of input. For example, the command WC statistic specifies the number of rows, words, and characters that a file contains. If you type only on the shell command line:
$WC
The WC will wait for the user to tell it what to count, when the shell looks like it's dead, all the text typed from the keyboard appears on the screen, but nothing happens until the is pressed, and the WC writes the command results on the screen. If you give a file name as an argument to the WC command, as the following example shows, the WC returns the number of rows, words, and characters that the file contains.
$ wc/etc/passwd
726/ETC/PASSWD another way to pass the contents of a/etc/passwd file to a WC command is to redirect the input of the WC. The general form of input redirection is: command < filename. You can redirect the input of the WC command to the/etc/passwd file using the following command:
$ WC </etc/passwd
20 23 726
Another type of input redirection, called the here Document, tells the shell that the standard input for the current command comes from the shell command line. The redirect operator for here document uses <<. It enters the text redirection between a pair of delimiters (represented by Delim in this example) to the command. The following example takes the body of a pair of separator delim as input to the WC command, counting the number of lines, words, and characters in the body.
$ wc<
>this Text forms the content
>of the Here Document,which
>continues until the end of
>text Delimter
>delim
After the << operator, any character can be used as a separator before the body begins, in this case using Delim as the separator. The body of Here document continues until you meet another separator. The second delimiter should appear at the beginning of the new line. The body of this here document (excluding the start and end delimiters) is redirected to the command WC as its standard input.
Use of the shell command line above.