Linux redirection details

Source: Internet
Author: User
Tags egrep
Article Title: Linux redirection details. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

I/O redirection details and Application Instances

1. Basic concepts (this is the prerequisite for understanding the following knowledge. please be sure to understand it)

A, I/O redirection is usually related to FD, and shell FD is usually 10, that is, 0 ~ 9;

B. There are three commonly used FD types: 0 (stdin, standard input), 1 (stdout, standard output), and 2 (stderr, standard error output ), the default value is related to keyboard, monitor, and monitor;

C. Use <to change the reading data channel (stdin) so that it can read from the specified file;

D. Use> to change the output data channel (stdout, stderr) and output it to the specified file;

E, 0 is the default value of <, so <and 0 <是一样的;同理,> It is the same as 1>;

F. In IO redirection, The stdout and stderr pipelines are prepared before reading data from stdin;

G. Pipeline "|" (pipe line): stdout of the previous command receives stdin of the next command;

H. The tee Command copies stdout to the file without affecting the original I/O;

I. bash (ksh) Command Execution Process: Analysis command-variable evaluate-command substitution (''and $ ()-redirect-Wildcard expand-confirm path-Execute Command;

J. () Place the command group in sub-shell for execution, also known as nested sub-shell. It has a very important feature: Inherit the Standard input, output, and, and error plus any other open file descriptors.

K. exec command: It is often used to replace the current shell and restart a shell. In other words, there is no starting shell. When you use this command, any existing environment will be cleared. When exec operates on file descriptors, it is only in this case that exec will not overwrite your current shell environment.

2. Basic IO

Cmd> file: redirects stdout to the file;

Cmd> file: redirects stdout to the file (append );

Cmd 1> fiel redirects stdout to the file;

Cmd> file 2> & 1 redirects stdout and stderr together to the file;

Cmd 2> file: redirects stderr to the file;

Cmd 2> file redirects stderr to the file (append );

Cmd> file 2> & 1 redirect stderr and stderr together to the file (append );

Cmd <file> the file2 cmd command uses the file as stdin and the file2 file as stdout;

Cat <> file: open the file in read/write mode;

Cmd <file cmd command uses file as stdin;

Cmd <delimiter Here document, read from stdin until the delimiter is encountered.

3. Advanced IO

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

<& N standard input copied from file descriptor n;

<&-Disable the standard input (keyboard );

> &-Disable standard output;

N <&-indicates that n is disabled;

N> &-indicates that output n is disabled;

All the above forms can lead to a number. In this case, the created file descriptor is specified by this number rather than the default 0 or 1. For example:

... 2> run a command in file and direct the error output (file descriptor 2) to file.

... 2> & 1 run a command and merge its standard output and output. (Strictly speaking, file descriptor 2 is created by copying file descriptor 1, but the effect is usually that two streams are merged .)

For details about 2> & 1: 2> & 1, that is, FD2 = FD1. This does not mean that the value of FD2 is equal to the value of FD1, because> is to change the data channel sent, that is to say, change the "data output channel" of FD2 to "data output channel" of FD1 ". In this case, this change seems to have no effect, because the default output of FD2 and the default output of FD1 are both monitor, the same! However, when FD1 is another file or even another FD, this has a special purpose. Be sure to understand this.

Exec 0 exec 1> outfilename # open the file outfilename as stdout.

Exec 2> errfilename # open the file errfilename as stderr.

Exec 0 <&-# disable FD0.

Exec 1> &-# disable FD1.

Exec 5> &-# disable FD5.

Q: What are the consequences of disabling FD0, FD1, and FD2? What is the difference between restoring FD0, FD1, and FD2 and disabling FD0, FD1, and FD2? What are the codes? FD3 ~ Enabled ~ FD9, after we use it, do you think it is to close them or restore them?

The following is a prompt (the example is from the CU post. If you forget the source, add it in the future ):

Exec 6> & 2 2> ver command> dev/null & exec 2> & 6 # restore FD2

4. Simple examples

A, stdout, and stderr are both sent to egrep through the pipeline:

(Ls you no 2> & 1; ls yes 2> & 1) 2> & 1 | egrep \ *> file (ls you no 2> & 1; ls yes 2> & 1) | egrep \ *> file (ls you no; ls yes) 2> & 1 | egrep \ *> file

In this example, note the following:

Explain the command execution sequence and pipeline "|": Before executing the command, you must first perform redirection and connect the stdout of the nested sub-shell to the stdin of the egrep command. The nested sub-shell can be considered as a command by adding () to the two commands in. Its FD1 has been connected to "|" and sent to egrep. In the case of 2> & 1, FD2 = FD1, that is, FD2, like FD1, is sent to the pipeline "|.

B. Nothing is sent to egrep through a pipeline, and all is sent to monitor. (Ls you no 2> & 1; ls yes 2> & 1)> & 2 | egrep \ *> file. Although FD2 is transferred to FD1 in (), when> & 2 is encountered outside (), all results are sent to monitor. Please understand:

(Ls you no 2> & 1) 1> & 2 | egrep \ *> file # send to monitor ls you no 2> & 1 1> & 2 | egrep \ *> file # send to MPs queue "|" ls you no 1> & 2 2> & 1 | egrep \ *> file # sent to monitor

5. Intermediate order example

Condition: stderr sends the message to egrep through the pipeline, and the correct message is still sent to monitor (unchanged)

Exec 4> & 1; (ls you no 2> & 1 1 1> & 4 4> &-; ls yes 2> & 1 1> & 4 4 4> &-) | egrep \ *> file; exec 4> &-or exec 4> & 1; (ls you no; ls yes) 2> & 1 1> & 4 4> &-| egrep \ *> file; exec 4> &-

If two conditions are added:

(1) concurrent operation of cmd1 and cmd2 is required;

(2) Assign the return value of cmd1 to the variable ss.

Then:

Exec 3> & 1; exec 4> & 1 ss =$ (ls you no 2> & 1 1> & 3 3> &-; echo $? > & 4) | egrep \ *> file) 4> & 1) exec 3> &-; exec 4> &-

Note:

Exec 3> & 1; 4> & 1 create FD3, which is used to restore FD1 from the following ls Statement (sub-shell) to FD1, that is, output to monitor, you can regard FD3 as the initial FD1 hard disk backup (that is, output to monitor); Create FD4 and use it to save the return value of ls (echo $ ?), You can think of FD4 as a store for computing "echo $?" during the test ?" Draft paper;

(Ls you no 2> & 1 1> & 3 3> &-; echo $? > & 4) Remember the sub-shell and pipeline mentioned above. This command First inherits from FD0, FD1, FD2, FD3, and FD4, which are located before the pipeline. Therefore, before running the command, the FD1 of the sub-shell is connected to the pipeline "|. However, the condition is that stderr is sent to egrep through the pipeline, and stdout is still output to monitor. Therefore, through 2> & 1, The FD1 pipeline of the sub-shell is "sent to" FD2 ", so the stderr in the sub-shell is sent to the pipeline" | ", and then through 1> & 3, restore the previous "hard disk backup" to the FD1 of the sub-shell, so the FD1 in the sub-shell is sent to monitor. 3> &-, close 3, and run echo $? , The output value should have been sent to the pipeline, and the output is sent to "draft paper" FD4 through> & 4, and left for backup.

(Ls you no 2> & 1 1> & 3 3> &-; echo $? > & 4) | egrep \ *> file). Therefore, stderr sends the data to egrep through the pipeline, and stdout sends the data to monitor. However, if there is FD4, where does it go? $ (Ls you no 2> & 1 1> & 3 3> &-; echo $? > & 4) | egrep \ *> file) 4> & 1) the last 4> & 1 means to redirect FD4 to FD1. However, because it is output in $ (), its value is assigned to the variable ss. Disable FD3 and FD4 in the last line.

6. Advanced examples

Command cmd1, cmd2, cmd3, IPv4. how to use one-way pipeline to complete the following functions:

1. Execute all commands in parallel.

2. stdin is not required for cmd1 and cmd2.

3. stdout of cmd1 and cmd2 is directed to stdin of cmd3.

4. stderr of cmd1 and cmd2 is directed to stdin of limit 4.

5. stdout of cmd3 is directed to file a and stderr is directed to the screen.

6. stdout of IPv4 is directed to file B, and stderr is directed to the screen.

7. The return code of cmd1 is assigned to variable s.

8. Temporary files cannot be used.

Solution:

Exec 3> & 1; exec 4> & 1 s =$ (cmd1 1> & 3; echo $? > & 4) | cmd2) 3> & 1 | cmd3> a 2> & 3) 2> & 1 | 1_4> B) 4> & 1) exec 3> &-; exec 4> &-

This is a step-by-step explanation (complicated, I feel clear about it. After a while, I will see it again. The brain is still blank for a few minutes ~~~, I did not expect it to be clear. Exec 3> & 1; exec 4> & 1 the previous example shows that FD3 is created, FD1 is restored for cmd1, FD2 is restored for cmd3, and FD4 is created, save "echo $?" The "draft" of the output value ".

The first pair of parentheses: (cmd1 1> & 3; echo $? > & 4) and the following (first) pipelines. In the first bracket (sub-shell), its FD1 has been connected to the pipeline, so use FD3 to restore FD1 to normal, so it is not allowed to run to the pipeline; here cmd1 does not have stdin, then, save the Returned Code of cmd1 to fd4.

Parentheses (cmd1 1> & 3; echo $? > & 4) | cmd2) 3> & 1 and later (second) pipelines. The above FD1 is no longer sent to cmd2, and FD2 is not sent by default, so cmd2 does not have stdin, so in the second pair of brackets: stdout and stderr of cmd1 and cmd2 are default outputs, until "3> & 1. Note: "3> & 1" first shows a command in the second pair of brackets. When they encounter the second pipe, their FD1 is connected to the pipe "| ", due to the role of "3> & 1", the FD1 of the sub-shell is sent to FD3 for use, so all FD3 outputs are "streamed to" cmd3, because of the inheritance relationship (inheriting the first line of command), FD3 is actually the stdout of cmd1 and cmd2, so "stdout of cmd1 and cmd2 is directed to stdin of cmd3"

Brackets 3: (cmd1 1> & 3; echo $? > & 4) | cmd2) 3> & 1 | cmd3> a 2> & 3) 2> & 1 and the third MPs queue. Stdout of cmd1 and cmd2 has been directed to stdin of cmd3. After processing, cmd3> a means to send its stdout to file. 2> & 3 indicates that the error output of cmd3 recovery is FD3, which is sent to monitor. Therefore, "cmd3 stdout is directed to file a, and stderr is directed to the screen ". If "2> & 3" is not available, the error output of cmd3 will interfere with the error output of cmd1 and cmd2, so it is required! Note that "2> & 1" after the third pair of brackets | the FD1 of its sub-shell is connected to the pipeline "|", but the sub-shell FD1 is generous and sent to FD2, therefore, FD2 is connected to the MPs queue. Do you still remember the previous cmd1 and cmd2? Their stderr never moved. So here, the fourth command "limit 4" is sent through the pipeline. That is, "stderr of cmd1 and cmd2 is directed to stdin of limit 4 ". It is relatively simple later. When 4> B indicates "when 4 stdout is directed to file B, stderr is directed to the screen (default )"

Brackets 4: (cmd1 1> & 3; echo $? > & 4) | cmd2) 3> & 1 | cmd3> a 2> & 3) 2> & 1 | 1_4> B) and the next 4> & 1. Four pairs of FD1 and FD2 in brackets are all processed. But remember the previous "echo $? > & 4 "is the draft paper? "4> & 1" is used to "Send the content on the draft to monitor", but because there is $ () on the outermost side, it is "wrapped ". Therefore, the value is assigned to the variable "s ".

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.