Summary of Linux exec usage

Source: Internet
Author: User

First, EXEC summary

EXEC command

Role

EXEC ls

Do not return to the original shell after executing ls,ls in the shell.

EXEC <file

Content in file as standard input (instead of stdin)

EXEC >file

Write standard output to file (instead of stdout)

EXEC 3<file

The file is read into the file Descriptor 3 (at this point, the filename descriptor 3 is created)

Sort <&3

Use the file Descriptor 3 as a temporary input for sort sorting

EXEC 4>file

Writes the contents of the write file descriptor 4 to the file (at this point, the filename descriptor 4 is created)

LS >&4

LS will not be displayed, directly written to the file descriptor 4, which is

EXEC 5<&4

Create a copy file descriptor for file Descriptor 4 5

EXEC 3<&-

Close File Descriptor 3

Ii. Example of EXEC use

2.exec I/O redirection detailed and application examples

2.1 Basic Concepts

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, 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error Output), the default and keyboard, monitor, monitor;

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

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

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

G, 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.2. cmd &n using System Invoke DUP (2) to copy the file descriptor N and use the result as standard output

&-Turn off standard output

n&-means the n output is turned off

All of the above can be preceded by a number, at which time the file descriptor is specified by this number instead of the default of 0 or 1. Such as:

... 2>file runs a command and directs the error output (file descriptor 2) to file.

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

We explain 2>&1 in detail: 2>&1 is fd2=fd1, this is not to say that the value of FD2 equals FD1 value, because > is to change the data channel sent, that is, FD2 "data output channel" to FD1 "data transmission Out channel ". If that's the case, the change doesn't seem to work, because the default output of FD2 and the default output of FD1 are all monitor, the same!

However, when FD1 is a different file, or even other FD, this has a special purpose. Please make sure you understand this.

2.3, if stdin, stdout, stderr was redirected or closed, but did not save the original FD, can you restore it to the default state?

If stdin is turned off because it causes an exit, it must not be recovered.

If you redirect or close one of the stdout and stderr, you can recover because they are sent to monitor by default (but do not know if there are any other effects). For example, restore redirected or closed stdout:exec 1>&2, restore the redirected or closed stderr:exec 2>&1.

If stdout and stderr are all closed, and the original FD is not saved, you can use: Exec 1>/dev/tty recovery.

2.4, cmd >a 2>a and cmd >a 2>&1 why different?

CMD >a 2>a:stdout and stderr are sent directly to file A, a file is opened two times, resulting in stdout and stderr covering each other.

CMD >a 2>&1:stdout is sent directly to file A, stderr is inherited from the FD1 pipeline and is then sent to file a. A file is opened only once, which is FD1 to open it.

I think: the difference between them is:

The cmd >a 2>a is equivalent to using two channels that compete with each other to use file A;

The cmd >a 2>&1 uses only one pipe, but it already includes stdout and stderr at its source.

The efficiency of the CMD >a 2>&1 should be higher in terms of IO efficiency!

EXEC 0

EXEC 1>outfilename # Open file Outfilename as stdout

EXEC 2>errfilename # Open file Errfilename as stderr

EXEC 0&-# Close FD1

EXEC 5>&-# Close FD5

Turn from: http://blog.csdn.net/wangbaochu/article/details/42707739, thanks!

Summary of Linux exec usage

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.