65. Shell, input/output redirection

Source: Internet
Author: User

1. Redirect related file descriptors

1) file descriptor

①0 standard input,/dev/stdin->/proc/self/FD/0

②1 standard output,/dev/stdout->/proc/self/FD/1

③2 standard error output,/dev/stderr->/proc/self/FD/2

2) redirection

①> Is 1>

②<0 <

③> Write command output to a file or device (such as a printer) instead of a command prompt window.

④> Add the command output to the end of the file without deleting the information in the file.

⑤> & Write the output of one handle to the input of another handle.

2> & 1 declares that file descriptor 2 is a copy of file descriptor 1 (strictly speaking, file descriptor 2 is created by copying file descriptor 1, but the effect is usually that two streams are merged ).

3) Others

①> After file appends the stdout string to the file content

②> File redirects stdout to a new file. When $ noclobber is set, the file can be rewritten.

③> & File: redirects stdout and stderr (/dev/stdout) to a file (command> & file)

④>>& Append the stdout and stderr (/dev/stderr) strings to the file content (command >>> & file)

⑤[N]> &-Disable stdout or file descriptor N (command> &-)

2. Note

1) if the shell encounters the ">" operator, it will determine whether the file on the right exists. If it exists, it will first delete it and create a new file; if it does not exist, it will be created directly. No matter whether the command on the left is executed successfully or not, the file on the right is empty.

2) ">" operator to determine the file on the right. If the file does not exist, create it first. When you open a file by adding the file, a file descriptor [unspecified, default: 1, 2] is assigned and then bound to the standard output or error output on the left.

3) when the command is executed, the descriptor of the bound file automatically becomes invalid, and 0, 1, and 2 are idle again.

4) before executing a command, the system checks whether the output is correct. If the output device is incorrect, the system does not execute the command.

5) in Io redirection, The stdout and stderr pipelines are prepared before reading data from stdin.

6) if tee does not affect the original I/O, copy the stdout file to the file.

7) bash Command Execution Process: Analysis command-variable evaluation-command substitution (''and $ ()-redirection-Wildcard expansion-confirm path-Execute Command

8) () Place the Command Group in sub-shell for execution, also known as nested sub-shell. One important feature is that it inherits the standard input, output, and error plus any other open file descriptors.

3. Example

1) Cat> catfile <test. Sh # Cat obtains the input data from test. SH and then outputs the data to the catfile.

2) Cat> catfile # Get data from the standard keyboard and output it to catfile

3) Cat> catfile <EOF # Here, "<", indicating "ending input character ". When an empty line is entered with the EOF character, the input ends automatically without Ctrl + d

4) Cat <> file opens the file in read/write mode;

5) CMD <delimiter> file, which is read from stdin until the delimiter Delimiter is encountered.

4. Exec

1) EXEC command: It is often used to replace the current shell and restart a shell, that is, there is no promoter shell. When you use this command, any existing environment will be cleared. Exec does not overwrite the current shell environment when operating the file descriptor.

2) Command Format

Exec file descriptor [N] <or> file or file descriptor or device

3) as described above, the input and output are redirected. After the input and output are bound to a file or device, only the current command is valid, if all the following commands are supported, use the EXEC command.

5. Exec example

1) exec 6> & 1 # bind the standard output to fd 6

Ls/proc/self/FD/

2) exec 1> & 6 # restore standard output

3) exec 6> &-# disable FD 6 Descriptor

Reference

[1] MAN Manual

[2] Good Summary

Http://hi.baidu.com/xxjyz/blog/item/59bbbb0394856d1c4bfb51ee.html

Http://blog.163.com/zhoumhan_0351/blog/static/39954227201061045630711/

[3] haowen

Http://www.360doc.com/content/11/0720/15/379588_134716249.shtml

[4] clear explanations

Http://www.xxlinux.com/linux/article/development/shell/2006-10-16/5018.html

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.