I/O redirection related in the Linux shell

Source: Internet
Author: User
Tags stdin egrep

1, the basic concept (this is the premise of understanding the knowledge behind, it must be understood)

A, I/O redirection is usually related to FD, the Shell's FD is usually 10, namely 0~9;

b, there are 3 commonly used FD, for0 (stdin, standard input)1 (stdout, standard output)2 (stderr, standard error output), the default is related to keyboard, monitor, monitor;

C, the use of < to change the read Data channel (stdin), so that it from the specified file read into;

D, using > To change the Sent data channel (stdout, stderr), so that it output to the specified file;

E, 0 is the default value of <, so < is the same as 0<;,> is the same as 1>;

F, in IO redirection, stdout and stderr pipeline will be ready before the stdin read into the data;

G, piping "|" (Pipe line): The stdout of the previous command received the stdin of the next command;

H, Tee command is in the case without affecting the original I/O, the stdout copy to the file;

I, Bash (ksh) the process of executing commands: Parse command-variable Evaluation-command substitution ("and $ ())-redirect-wildcard expansion-Determine path-execute command;

J, () put command group in Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell's standard input, output, and error plus a NY other Open file descriptors.

K, EXEC command: Often used to replace the current shell and restart a shell, in other words, does not start the child shell. Any existing environment will be purged when this command is used. exec does not overwrite your current shell environment when working with file descriptors, and only then.



2. Basic IO

cmd > file redirect stdout to file;    cmd 1> fiel redirect stdout to file;  cmd > file 2>&1 redirect stdout and stderr to file files;  cmd 2> file to redirect stderr to files;  cmd 2>> file redirect stderr to File (append),  cmd >> file 2>&1 redirect stderr and stderr to file (append);  cmd < file >file2 cmd command with file as stdin, file2 file as stdout;  cat <>file Open file in read-write mode;  cmd < file cmd command with file as St din;  cmd << delimiter here document, read in from stdin until a delimiter delimiter is encountered.  


3. Advanced IO

>&n 使用系统调用 dup (2) 复制文件描述符 n 并把结果用作标准输出; <&n 标准输入复制自文件描述符 n; <&- 关闭标准输入(键盘); >&- 关闭标准输出; n<&- 表示将 n 号输入关闭; n>&- 表示将 n 号输出关闭; 上述所有形式都可以前导一个数字,此时建立的文件描述符由这个数字指定而不是缺省的 0 或 1。如: ... 2>file 运行一个命令并把错误输出(文件描述符 2)定向到 file。 ... 2>&1 运行一个命令并把它的标准输出和输出合并。(严格的说是通过复制文件描述符 1 来建立文件描述符 2 ,但效果通常是合并了两个流。) 我 们对 2>&1详细说明一下 :2>&1 也就是 FD2=FD1 ,这里并不是说FD2 的值 等于FD1的值,因为 > 是改变送出的数据信道,也就是说把 FD2 的 “数据输出通道” 改为 FD1 的 “数据输出通道”。如果仅仅这样,这个改变好像没有什么作用,因为 FD2 的默认输出和 FD1的默认输出本来都是 monitor,一样的! 但是,当 FD1 是其他文件,甚至是其他 FD 时,这个就具有特殊的用途了。请大家务必理解这一点。 exec0exec 1>outfilename # 打开文件outfilename作为stdout。 exec2>errfilename # 打开文件 errfilename作为 stderr。 exec0<&- # 关闭 FD0。 exec1>&- # 关闭 FD1。 exec5>&- # 关闭 FD5。

Q: What happens if FD0, FD1, FD2 are closed? What is the difference between recovering FD0, FD1, FD2 and closing FD0, FD1, and FD2? What are the codes, respectively? Open the FD3~FD9, after we run out, do you think it is to shut them down or restore?

Here is a hint (example from a post of Cu, forget the source, and then make up):



3 EXEC 2>&6 # recovery FD2


4. Simple example

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



3 (ls you no;ls Yes) 2>&1|egrep \* >file

This example should be noted that:

Understanding command execution Order and piping "|" : Before the command executes, the redirect is processed and the stdout of the nested Sub-shell is received stdin of the Egrep command.
Nested Sub-shell, two commands in () plus (), can be thought of as a command. Its FD1 has been connected to the "|" Sent to Egrep, when encountered 2>&1, that is, fd2=fd1, that is, FD2 with FD1, toward the pipeline "|" Send it over there.

b, nothing through the pipeline to Egrep, all sent to monitor. (ls you no 2>&1;ls yes 2>&1) >&2|egrep \* >file. Although the FD2 is transferred to the FD1 in (), but outside the (), encountered >&2, the results are all sent to monitor. Please understand:



3 ls You no 1>&2 2>&1|egrep \* >file # # Send to monitor



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


5, the middle-order example
Condition: stderr is sent to Egrep through the pipeline, the correct message is still sent to monitor (unchanged)
If you add two conditions:

(1) Require CMD1 and cmd2 parallel operation;

(2) Assign the return value of the CMD1 to the variable SS.

The following is:



EXEC 3>&-;exec 4>&-

Description

exec 3>&1;4>&1 establishes FD3, which is used to restore FD1 in the following LS (sub-shell) to normal FD1, which is output to monitor, You can think of FD3 as the most original FD1 hard disk backup (i.e. output to monitor), FD4, to be used as the return value of the Save LS (echo $?), you can consider FD4 as your test for storing the calculation "echo $?" The grass manuscript;

(ls you no 2>&1 1>&3 3>&-;echo $? >&4) Remember the child shells and pipes that you said earlier. This command first inherits FD0, FD1, FD2, FD3, and FD4, which is located in front of the pipeline, so the Shell's own FD1 and pipelines are first run before the command "|" Connected. But our condition is that the stderr is sent through the pipeline to egrep,stdout still output to monitor. So through the 2>&1, first the shell of the FD1 pipe "to" FD2, so the child shell StdErr sent to the pipeline "|" Again, the former "hard disk backup" is restored to the FD1 of the child shell by 1>&3, so the FD1 in the child shell becomes sent to monitor. Then pass the 3>&-, turn 3 off, and then run echo $? , its output value should be sent to the pipeline, through the >&4, the output sent to the "draft paper" FD4, left to spare.

(ls you 2>&1 1>&3 3>&-;echo $ >&4) |egrep \* >file) so stderr sent Egrep through the pipeline, stdout to Mo Nitor, but, FD4, where did it go? $ ((ls you no 2>&1 1>&3 3>&-;echo $ >&4) |egrep \* >file) 4>&1) The last 4>&1 is to put F D4 Redirect to FD1. But because its output is in $ (), its value is assigned to the variable SS. The last line closes FD3, FD4.

6. Advanced Examples

command cmd1, CMD2, Cmd3, CMD4. How to use a one-way pipeline to complete the following functions:

1. All commands are executed in parallel.

2. Cmd1 and CMD2 do not require stdin.

3. The stdout of CMD1 and CMD2 are directed to the stdin of CMD3.

4. The stderr of Cmd1 and CMD2 are directed to the stdin of CMD4.

5. Cmd3 stdout directed to file A, stderr directed to the screen.

6. CMD4 stdout directed to File B, stderr directed to the screen.

7. The return code of the CMD1 is assigned to the variable s.

8. Temporary files cannot be used.

Workaround:


S=$ ((((Cmd1 1>&3; echo $? >&4) | cmd2) 3>

EXEC 3>&-; EXEC 4>&-




This I step by step explanation (good complex, I feel understand, after a while to see, the brain still has a few minutes blank ~ ~ ~, did not think I can see understand. EXEC 3>&1; EXEC 4>&1 Previous examples have explained, is to establish FD3, to cmd1 restore its FD1 with and to CMD3 restore its FD2 use, establish FD4, save "echo $?" The "draft paper" for the output value.

First pair of parentheses: (Cmd1 1>&3; echo $? >&4) and subsequent (first) pipelines. In the first parenthesis (child shell), its FD1 has been connected to the pipeline, so with FD3 will FD1 back to normal, do not let him run to the pipeline, where the Cmd1 no stdin, and then save the cmd1 run return code to FD4.

Second pair of brackets: ((cmd1 1>&3; echo $? >&4) | cmd2) 3>&1 and subsequent (second) pipelines. The front of the FD1 has not been sent to CMD2, FD2 default is not sent over, so CMD2 also did not stdin, so in the second pair of parentheses inside: cmd1 and CMD2 stdout, stderr for the default output, has been encountered "3>&1" so far. Note: "3>&1", the second pair of parentheses first see a command, they encountered a second pipe, its FD1 connected to the pipe "|", because of the role of "3>&1", the FD1 of the child shell to FD3 use, so all FD3 output "flow to" Cmd3, and because of inheritance (the command that inherits the first line), FD3 is actually stdout of CMD1 and CMD2, and cmd1 of CMD2 and StdOut is directed to Cmd3 stdin.

The third pair of brackets: (((cmd1 1>&3; echo $? >&4) | cmd2) 3>&1 | cmd3 >a 2>&3) 2>&1 and the third pipeline thereafter.
Cmd1 and CMD2 's stdout have been directed to Cmd3 stdin, after processing, cmd3 >a means to send it stdout to a file. And 2>&3 means: The recovery cmd3 error output is FD3, that is sent to monitor.
So "Cmd3 stdout directed to file A, stderr directed to the screen." If there is no "2>&3", then cmd3 error output will interfere with CMD1 and CMD2 error output, so it is necessary! Note the "2>&1" after the third pair of parentheses | , the FD1 of its child shell was originally connected to the pipe "|", but the child shell FD1 Generous, gave FD2, so FD2 connected to the pipeline.
Remember the CMD1 and CMD2 in front of you? Their stderr has not moved. So here, through the pipeline gave the fourth command CMD4. That is, "Cmd1 and Cmd2 stderr directed to Cmd4 stdin".
It's easier in the back. CMD4 >b means "Cmd4 stdout directed to File B, stderr directed to screen (default)"

Four pairs of brackets: ((((Cmd1 1>&3; echo $? >&4) | cmd2) 3>&1 | cmd3 >a 2>&3) 2>&1 | CMD4 >b ) and the subsequent 4>&1.
Four pairs of parentheses inside the FD1, FD2 have finished processing. But remember the front "echo $?" >&4 "That piece of paper"? The role of "4>&1" is to "send the contents of the draft paper to monitor", but because there are $ () on the outside, it is "wrapped".
The value is then assigned to the variable "s".

Reprint to: http://www.xxlinux.com/linux/article/development/shell/2006-10-16/5018_2.html

Http://www.cnblogs.com/huangzhen/archive/2011/08/21/2147834.html

I/O redirection correlation in the Linux shell (RPM)

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.