Data stream redirection in Linux

Source: Internet
Author: User
A glimpse of data stream redirection in Linux 1. 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 data stream redirection in Linux. First, let's talk about the concept of data stream before talking about data stream redirection. 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): The code is 0, use <或者<<2)标准输出(stdout):代码为1,使用> 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">? 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 <和<<分别表示0<和0<<。 3、标准输出数据流重定向详解 在说输入之前,我先跟大家说说输出吧,一般使用例子都能让人更容易明白一样东西是怎么样使用的。所以请看下面的例子,在当前目录下,已存在一个名为streamtest.txt的文本件,我使用cat命令来进行测试,我们知道,cat命令会把文本文件中的内容输出到屏幕上来。请看下面的例子:  我们可以看到直接使用cat命令,streamtest.txt文件的内容直接打印在屏幕上。 而第二条语句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. detailed explanation of standard input data stream rewriting in standard input, <代表将原来需要由键盘输入的数据改由文件内容来替代,<<则代表结束输入。例如:利用cat来创建一个简单的文件catfile1,如所示:  如果当前目录下不存在catfile1,则系统自动创建这个文件,并由键盘向这个文件写入内容;若文件已存在,则用输入的内容覆盖该文件中的内容。而标准输入重定向可以用文件的内容来替代键盘输入,其操作如所示: 可以看到,它把catfile1的内容输入到catfile2中,代替先前的键盘输入。 值得注意的是,在标准输入重定向中<<并不表示追加,而是表示结束输入的意思,即作为一个结束符。请看如下的例子: 之前的例子,都是使用ctrl+d来结束的,而这个例子,当你输入“eof”时,自动退出了,当然这个结束符,你可以自己定义。这对写程序是有一定的好处的。 三、把正确信息与错误信息写入同一个文件中的方法上面的例子,都是把正确和错误的信息分别存储在不同的文件上的,那有没有办法把正确和错误的信息都储存在同一个文件中呢?当然有!请看下面的例子:  注意:上面第三行的命令是错误的,虽然它看看起来非常直观。它的错误并不是因为语法之类的错误,也不是因为它不能执行,而是因为,有两条数据流同时写入一个文件,又没有特殊的语法,此时会导致两条数据流交叉地写入文件,造成次序的错乱,而不是屏幕上原来输出的排序,使文件的信息不可读。 四、屏蔽所有信息的方法如果我有一定的目的,想把所有的错误信息或正确信息都屏蔽掉,可以怎么办呢?这时可以利用数据流重定向,使它流向一个设备—— dev null,它就像一个黑洞一样,可以吃掉任何导向这个设备的信息。请看下面的例子: 无论是正确的信息还是错误的信息,它都能吃掉。 五、何时使用数据流重定向既然我们知道了数据流的重定向,那么什么时候要使用数据流重定向呢?数据流重定向一般应用于:1、屏幕输出的信息很重要,而且我们需要将它保存下来;2、后台执行中的程序,不希望它干扰屏幕正常的输出结果;3、一些系统的例行命令的执行结果,希望它能够保存下来;4、一些执行命令的可能已知出错信息时,想以“2> /Dev/null. 5. output error messages and correct information respectively;
Related Article

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.