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

发布于 2024-12-27 11:46:48 字数 542 浏览 3 评论 0原文

我看到一个 bash 脚本,其中的函数中有 exec 1>&2 命令。就像这样:

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

已下线请稍等 2025-01-03 11:46:48

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

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

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

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

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

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

exec is 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:

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

(That's quoting from the message given by 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.)

风柔一江水 2025-01-03 11:46:48

这是历史的偶然,没有什么充分的理由。

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

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

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

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.

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

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").

缺⑴份安定 2025-01-03 11:46:48

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

exec

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

注意 exec N >文件名影响整个脚本或当前 shell。从那时起,脚本或 shell 的 PID 中的重定向已发生变化。然而,N>1。 filename 仅影响新分叉的进程,而不影响整个脚本或 shell。

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

Original source: 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.

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文