Shell redirection under Linux

Source: Internet
Author: User

1. Standard input, standard output and standard error output

Linux system open 3 files, standard input, standard output, standard error output.

Standard input: Enter data from the keyboard, i.e. read data from the keyboard.

Standard output: Outputs the data to the terminal.

Standard error Output: Outputs the standard error to the terminal.

The default standard input refers to the keyboard, the default standard output and the standard error output refer to the screen or the terminal.

The system assigns the file identifier FD (Descripter) to these three files.

In the Linux system, everything is a file, the operation of the file, the general need to use the file identifier. Their file identifiers, respectively, are 0,1,2.

2. Redirection

Redirection is divided into input redirection and output redirection.

The so-called input redirection is that the input is not read from the keyboard, but is entered from the file or other.

The so-called output redirection is not output to the terminal, but output to a file or other.

"<" represents the input redirection operator, and ">" represents the output redirection operator.

The input "<<" tells the shell that the current standard enters the middle of a pair of separators from the command line.

">>", with ">" Output, if the file does not exist will automatically create a file, if the second input, will overwrite the previous input, and ">>" means to append the second output to the file, rather than overwrite.

3. Take a look at a few examples below

(1) Input redirection

[Email protected] ~]# Wc</etc/inittab
53 229 1666

WC statistics Number of rows, words, characters

Where the WC input comes from/etc/inittab
[Email protected] ~]# WC <<AA
> 1
> 11
> 111
> AA
3 3 9

The WC statistic separators the contents between AA.

(2) Output redirection

[[email protected] ~]# Ps-ef >a.txt//redirect the information output of the process to the A.txt file

[[email protected] ~]# ps-ef> >a.txt//process information appended to the A.txt file

In fact, the so-called input and output redirection is not from the keyboard input and screen output.

4. Typical analysis of output redirection under Linux

(1) a.out > outfile 2>&1
(2) a.out 2> &1 >outfile

Analysis:

0 for standard input, 1 for standard output, and 2 for standard error output. where 1 (i.e. >=1>) can be omitted, the default is standard output.

(1) First redirect the a.out to the outfile file. The 2>&1 then redirects the standard error output to the standard output. The standard output is redirected at this point, which is equivalent to redirecting the standard output to the outfile with the standard error output. &1 represents the file descriptor for the standard output. 1 generally on the left side indicates where the standard output is redirected, and 1 on the right is redirected to the standard output, so use & to differentiate the file descriptor representing the standard output. 2>1 indicates that the standard error is redirected to file 1, and 2>&1 indicates that the standard error is redirected to the standard output.

(2) The standard error output is first redirected to the standard output, at which point the standard output is still in the terminal. Standard errors are therefore also in the terminal. Then the standard output goes to the outfile. The standard error is still in the terminal.

Therefore, (1) indicates that standard output and standard errors are outfile.

(2) indicates standard output to outfile, but the standard error output is still in the terminal.

LS 1>/dev/null 2>/dev/null

LS >/dev/null 2>&1

/dev/null is a trash bin that is a bottomless pit that represents the meaning of not showing.

That is, the standard output is not displayed with the standard error output.

The above two commands represent the same meaning.

4.exec binding Redirection

Format:

exec file descriptor [n] < or > file or filename descriptor or device

In the above mentioned input, output redirection will be input, output binding file or device after. It is only valid for the current instruction. If required after binding, all subsequent commands are supported. You need to use the EXEC command.

Instance:

[[email protected] shell]$ exec 6>&1 #将标准输出与fd 6绑定 [[email protected] shell]$ ls /proc/self/fd/ 0  1  2  3  6 #出现文件描述符6 [[email protected] shell]$ exec 1>suc.txt #将接下来所有命令标准输出,绑定到suc.txt文件(输出到该文件) [[email protected] shell]$ ls -al #执行命令,发现什么都不返回了,因为标准输出已经输出到suc.txt文件了 [[email protected] shell]$ exec 1>&6 #恢复标准输出 [[email protected] shell]$ exec 6>&- #关闭fd 6描述符 [[email protected] ~]$ ls /proc/self/fd/ 0  1  2  3

Note: Save the standard input to the file descriptor 6 before use, the description below, the file descriptor opens by default 0,1,2 can also use the custom descriptor. The standard output is then bound to a file, and then all of the output will occur to the file. After use, restore the standard output, close open File descriptor 6.

Interesting things:

Maybe a friend will use this: Exec 1>suc.txt, and then all the output is bound to the Suc.txt file, so how to restore the original? Try it and you'll find the problem ...

Resources:

http://blog.csdn.net/jfkidear/article/details/7823486

Http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html

Shell redirection under Linux

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.