Data flow redirection for "Linux" Linux

Source: Internet
Author: User

First of all, what is data flow redirection, so-called data flow redirection is simply a process, the process of capturing a file, or command, program, script, or even script code block of the output, and then the captured output, as input to another file, command, program, or a script.

When it comes to data flow redirection, we first need to understand the concept of file descriptors. For the Linux kernel, all open files are referenced by a file descriptor. The file descriptor is a non-negative integer. When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process, etc. By convention, the UNIX system Shell associates the file descriptor 0 with the standard input of the process, the file descriptor 1 is associated with the standard output, and the file descriptor 2 is associated with a standard error. Summarized as follows:

Standard input: File descriptor 0, usually referred to as keyboard input. Use symbols < or <<
Standard output: File descriptor 1, usually referred to as the output of the display. Use symbols > or >>
Standard error: File descriptor 2, usually also directed to the Monitor. Trial symbol 2> or 2>>

By default > and >> represent 1> or 1>>,< and << are equivalent to 0< and 0<<, respectively.

Standard output redirection: 

  #ls

Indicates that the current directory entry is listed and the result is output to the screen.

  #ls 1>file1

The above command redirects the standard output of the command to a file, not to the screen, and if the file identifier is not specified, the system defaults to 1, so 1 can be omitted.

That this command is equivalent to

  #ls >file1

If the file1 does not exist, the system is created automatically. If it already exists, the system first empties the file and then writes the data to the file. That is, the > output to an existing file, then overwrite this file back. If you do not want to overwrite, you can use >>. Represents appending the redirected data to the end of the File1 file.

Standard error Redirection:

  #ls-QW 2>errorfile

  Indicates that the error message is not output to the screen, but instead is written to ErrorFile. Note that the 2 here cannot be omitted. Because > is equivalent to 1>, the default is standard output redirection. So this is going to be written 2>, which indicates standard error redirection. -QW is used to generate error messages.

Write standard error and standard output to the same file:

#./a.out &>outfile

This command redirects the standard output and standard error of the./a.out to outfile. & here represents the standard error and standard output.

To close a file:

  "&-" means closing the file identifier

For information about closing file identifiers, refer to the following

n<&-Close input file identifier n0<&-or <&-close standard input stdinn>&-close output file identifier n1>&-or >&-off standard output stdout

  

To mask standard output or standard error:

./a.out >/dev/null #等同于./a.out 1>dev/null to mask standard output

./a.out 2>/dev/null #表示屏蔽标准错误

./a.out >/dev/null 2>/dev/null #表示同时屏蔽标准输出和标准错误

Open File:

#exec 3<>filename to           open the file filename and specify a file identifier of 3

Command J<>filename to open the file and indicate that the file identifier is J

Common REDIRECT commands:

cmd > File redirect stdout to file;

cmd >> file redirect stdout to file (append);

CMD 1> fiel redirect stdout to file;

cmd > File 2>&1 redirect stdout and stderr to file files;

cmd 2> file redirects stderr to file;

cmd 2>> file redirects stderr to file (append);

cmd >> file 2>&1 redirect stderr and stderr to file (append);

The cmd < file >file2 cmd command takes the file file as the stdin, with File2 as the stdout;

Cat <>file Open File in a read-write manner;

cmd < file cmd command with file as stdin;

CMD << delimiter here document, read in from stdin until the delimiter delimiter is encountered.

>&n uses the system invoke DUP (2) to copy the file descriptor N and use the result as standard output;

<&n standard input copied from file descriptor n;

<&-off standard input (keyboard);

>&-off standard output;

n<&-indicates that the n input is closed;

n>&-means the n output is closed;

Topics related to redirection:

This question comes from the advanced programming of the UNIX Environment (3rd edition) P73 3.5

In the Bourne shell, Bourne-again shell, and Korn shell, Digit1>&digit2 represents redirecting descriptor digit1 to the same file as the descriptor digit2. Please indicate the difference between the following two commands.

./a.out > OutFile 2>&1

./a.out 2>&1 >outfile

  

A: Because the shell handles the command line from left to right, you:

./a.out > OutFile 2>&1 equivalent to./a.out 1>outfile 2>&1

The result is that the standard output and standard error are set to the same file by first executing >outfile setting the standard output to outfile and then executing 2>&1, which calls the DUP to copy the standard output to descriptor 2 (standard error). That is, descriptors 1 and 2 point to the same file table entry. And for the command line

./a.out 2>&1 >outfile
The DUP is executed first, so the descriptor 20% is the terminal (assuming the command is interactive) and the standard output is redirected to outfile. The result is a descriptor 1 that points to the file table entry for outfile, descriptor 2 to the terminal's File table entry.

Resources:

1.http://os.51cto.com/art/201003/187688.htm

2.http://blog.csdn.net/ljianhui/article/details/9262737

3.https://www.ibm.com/developerworks/cn/linux/l-iotips/

4. Advanced programming of the UNIX environment

Command J<>filename to open the file and indicate that the file identifier is J

Data flow redirection for "Linux" 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.