bash 理解bash“exec 1>&2”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8888251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:30:03  来源:igfitidea点击:

understanding bash "exec 1>&2" command

bashexec

提问by Martin

I saw a bash script which had exec 1>&2command in a function. Something like:

我看到一个 bash 脚本,它exec 1>&2在函数中包含命令。就像是:

function example()
{
    exec 1>&2
    cat <<EOT
Script requires at least one parameter.
EOT
    exit 1
} 

As I understand, exec 1>&2means that everything from this point one will be directed to stderr. Is this some sort of fixed behaviour of execone needs to know by heart or is there some good explanation behind this? I mean as I understand, execin Bash script just invokes a commandtaking the same PID which the Bash script had and once the commandis finished, the PID is killed. 1>&2isn't a command. Could somebody explain the details(especially the whyquestion) behind the exec 1>&2command?

据我了解,这exec 1>&2意味着从这一点开始的所有内容都将被定向到stderr。这是exec需要记住的某种固定行为还是这背后有一些很好的解释?我的意思是,据我所知,Bash 脚本中的exec只是调用一个命令,该命令采用与 Bash 脚本相同的 PID,一旦命令完成,PID 就会被终止。1>&2不是命令。有人可以解释命令背后的细节(尤其是为什么的问题)exec 1>&2吗?

回答by ruakh

execis a built-in Bash function, so it can have special behavior that an external program couldn't have. In particular, it has the special behavior that:

exec是一个内置的 Bash 函数,因此它可以具有外部程序无法具有的特殊行为。特别是,它具有以下特殊行为:

If COMMAND is not specified, any redirections take effect in the current shell.

如果未指定 COMMAND,则任何重定向都会在当前 shell 中生效。

(That's quoting from the message given by help exec.)

(这是从 给出的消息中引用的help exec。)

This applies to any sort of redirection; you can also write, for example, any of these:

这适用于任何类型的重定向;例如,您还可以编写以下任何内容:

exec >tmp.txt
exec >>stdout.log 2>>stderr.log
exec 2>&1

(It does not, however, apply to pipes.)

(它不是,但是,适用于管道。)

回答by msw

The why is an accident of history, there isn't a great reason for it.

为什么是历史的偶然,没有什么大道理。

The redirection operators are applied to any program (for example, cat) and the >&form was added to express the "duplicate file descriptor 1 from 2" because it was useful. The >&syntax probably was used because they were running out of special characters and >&was nonsense given the basic meaning of &and >(but don't quote me on that). This stuff all dates back to the early Bourne shell around 1977.

重定向运算符适用于任何程序(例如,cat),并且>&添加了该形式以表达“从 2 复制文件描述符 1”,因为它很有用。使用该>&语法可能是因为它们用完了特殊字符并且>&考虑到&and的基本含义是无稽之谈>(但不要引用我的话)。这些东西都可以追溯到 1977 年左右的早期 Bourne shell。

Why execplus a redirection to alter the input and output of the current shell? Probably because there was already a meaning for the null command > pathwhich had the same effect as cat /dev/null > pathbut was easier to type, and when your teletype ran at 10 characters per second at best, that mattered. So the execbuilt-in was then overloaded: with no arguments, it applied the redirection operations to itself.

为什么要exec加上重定向来改变当前 shell 的输入和输出?可能是因为 null 命令已经有了一个含义,> path它与效果相同cat /dev/null > path但更容易输入,并且当您的电传打字机最多以每秒 10 个字符运行时,这很重要。因此,exec内置函数被重载:没有参数,它将重定向操作应用于自身。

Since bash tries to adhere to Bourne shell convention as much as possible, you get these antiquities which, as you suspect, you just have to remember especially if you are trying to impress other members of the appropriate sex (i.e. "chicks dig a guy who knows his i/o redirection arcana").

由于 bash 试图尽可能地遵守 Bourne shell 约定,因此您会得到这些古物,正如您怀疑的那样,您只需要记住,尤其是当您试图给其他合适性别的成员留下深刻印象时(即“小鸡挖知道他的 I/O 重定向奥秘”)。

回答by synthesizerpatel

Original source: http://tldp.org/LDP/abs/html/x17784.html

原始来源:http: //tldp.org/LDP/abs/html/x17784.html

An exec <filename command redirects stdin to a file. From that point on, all stdin comes from that file, rather than its normal source (usually keyboard input). This provides a method of reading a file line by line and possibly parsing each line of input using sed and/or awk.

exec <filename 命令将 stdin 重定向到文件。从那时起,所有标准输入都来自该文件,而不是其正常来源(通常是键盘输入)。这提供了一种逐行读取文件并可能使用 sed 和/或 awk 解析每一行输入的方法。

Similarly, an exec >filename command redirects stdout to a designated file. This sends all command output that would normally go to stdout to that file.

类似地, exec >filename 命令将标准输出重定向到指定的文件。这会将通常会转到 stdout 的所有命令输出发送到该文件。

Note that exec N > filename affects the entire script or current shell. Redirection in the PID of the script or shell from that point on has changed. However, N > filename affects only the newly-forked process, not the entire script or shell.

请注意, exec N > filename 会影响整个脚本或当前 shell。从那时起,脚本或 shell 的 PID 中的重定向已更改。但是, N > filename 仅影响新分叉的进程,而不影响整个脚本或 shell。

Further reading that will interest you can be found @ http://tldp.org/LDP/abs/html/io-redirection.html. There are lots of neat little redirection tricks you can do.

您可以在@ http://tldp.org/LDP/abs/html/io-redirection.html找到您感兴趣的进一步阅读。你可以做很多整洁的小重定向技巧。