Data Stream redirection in Linux

Source: Internet
Author: User
1. What is data stream redirection? before talking about data stream redirection, let's talk about the concept of data stream. There are three types of data streams: standard input (stdin), standard output (stdout), and standard error output (stderr ). To put it simply, standard output refers to the correct information returned by the command execution, while standard output refers to the error information returned after the command execution fails. This information is printed on the screen by default. So when is data stream redirection? Literally, it is to change the flow of data streams to a specified file or device. For example, the correct information (standard output information) returned by the command is sent to a file, and the returned error information (standard error output) is sent to another file, and save the information of the two files, without any information returned by the command execution on the screen. II. Implementation of Data Stream redirection 1. It is very simple to use shell in Linux to implement data stream redirection. Next we will introduce the redirection of the three data streams. 1) standard input (stdin): code 0, use <or <2) standard output (stdout): Code 1, use> or> 3) standard Error output (stderr): the code is 2. Use 2> or 2> here, you may still be confused about the role of the Code, what is the difference between <and <,> and>? 2. The purpose of the code is to distinguish the different data streams used in the command. For example, the 0 code is the standard input. Note that by default,> and> indicate 1> and 1>, respectively, while <and <indicate 0 <and 0 <respectively. 3. Explanation of standard output data stream redirection before talking about input, let's talk about output first. General examples can make it easier to understand how to use one thing. Take a look at the example below. There is a text file named streamtest.txt in the current directory. I use the cat command for testing. We know that the cat command will output the content in the text file to the screen. See the following example: we can see that the content of the streamtest.txt file is directly printed on the screen using the catcommand. The second statement cat streamtest.txt> outfile1 uses data stream redirection. What does this statement mean? It means to directly output the information generated by the cat command to the screen to the file outfile1 and save it. In addition, you can see that the information originally output on the screen is not output to the screen, because it is output to the file outfile1. In this way, you should be able to understand why data stream redirection is called. It is worth noting that if the outfile1 file does not exist in the current directory, the system will automatically create the file. If the file already exists in the current directory, the system first clears the content of the file and then writes the data to the file. That is to say, if you output the data to an existing file, the content of this file will be overwritten. Is there any way to prevent file content from being overwritten? Of course, that is, the difference between> and>. Its usage is similar to>. For example, if outfile2 does not exist in the current directory, the system automatically creates the file, if the file already exists in the current target, the data will be appended to the end of the file. So we can see two duplicate contents in the figure. Note: by default,> and> indicate 1> and 1> respectively, so 1> and 1> are not used in the command, because they are equivalent. 4. Explanation of redirection of standard error output data streams the usage of standard error output is similar to that of standard output. I will not repeat it here. The usage is as follows: Because teststream.txt does not exist under the current target, cat is used directly, the error message will be output on the screen. Of course, we can also use data stream redirection 2> to output it to the file errfile, rather than to the screen. Because> the default value is code 1, so we need to write it here as 2> so that the system will know that this is a standard error input redirection. Now you should be more deeply aware of the role of the Code. 2> is also the function of append. The preceding example shows that we can use data stream redirection to separate the error information from the correct information. 5. Rewrite the standard input data stream. In the standard input, <indicates that the data originally required by the keyboard is replaced by the file content. <indicates that the input is terminated. For example, use Cat to create a simple file catfile1, as shown in. If catfile1 does not exist in the current directory, the system automatically creates the file and writes content to the file on the keyboard; if the file already exists, use the input content to overwrite the content in the file. The standard input redirection can replace the keyboard input with the file content, as shown in the operation: You can see that it inputs the content of catfile1 into catfile2, instead of the previous keyboard input. It is worth noting that in the standard input redirection, <does not represent an append, but indicates the end input, that is, it is used as an Terminator. See the following example: in the previous example, CTRL + D is used to end the process. In this example, when you enter "EOF", the system automatically exits. Of course, this Terminator, you can define it yourself. This has some benefits for writing programs. 3. The above examples of writing correct information and error information into the same file store correct and error information on different files respectively, is there a way to store both the correct and wrong information in the same file? Of course! Please refer to the following example: Note: The command in the third line above is incorrect, although it looks very intuitive. Its error is not caused by errors such as syntax, nor because it cannot be executed, but because there are two data streams that write a file at the same time without special syntax, in this case, the two data streams are written to the file in a crossover manner, resulting in disordered order, rather than the sorting output on the screen, making the file information unreadable. 4. How can I block all information? What can I do if I want to block all error messages or correct information for a certain purpose? In this case, you can use data stream redirection to make it flow to a device --/dev/null. It is like a black hole and can eat any information directed to this device. See the following example: whether it is the correct information or the wrong information, it can be eaten. 5. When to use data stream redirection since we know Data Stream redirection, when should we use data stream redirection? Data Stream redirection is generally used in: 1. The screen output information is very important, and we need to save it; 2. The program in the background execution does not want it to interfere with the normal output results of the screen; 3. The execution results of some routine commands in some systems should be saved; 4. When some errors may be known during command execution, to block it with "2>/dev/null", 5. The error information and correct information must be output separately;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.