bash文件描述符重定向

Mic*_*ter 1 bash

我有以下两个bash脚本:

one.bash:

#!/bin/bash

echo "I don't control this line of output to stdout"

echo "I don't control this line of output to stderr" >&2

echo "I do control this line of output to fd 5" >&5
Run Code Online (Sandbox Code Playgroud)

callone.bash:

#!/bin/bash

# here I try to merge stdout and stderr into stderr.
# then direct fd5 into stdout.

bash ./one.bash 1>&2 5>&1
Run Code Online (Sandbox Code Playgroud)

当我像这样运行它: bash callone.bash 2>stderr.txt >stdout.txt

stderr.txt文件如下所示:

I don't control this line of output to stdout
I don't control this line of output to stderr
I do control this line of output to fd 5
Run Code Online (Sandbox Code Playgroud)

和stdout是空的.

我希望"do control"行只输出到stdout.txt.

进行更改的限制是:

  1. 我可以在callone.bash中更改任何内容.
  2. 我可以改变我控制的one.bash中的行.
  3. 我可以在与文件描述符5相关的one.bash中添加一个exec.
  4. 我必须按照指示运行脚本.

[编辑]这个用例是:我有一个脚本,可以执行其他脚本的各种运行,可以输出到stderr和stdout.但我需要确保用户只能看到控制良好的消息.所以我将受控良好的消息发送到fd5,其他所有内容(stdout和stderr)都发送到日志.

Eta*_*ner 7

重定向按顺序发生.

一旦你跑了,1>&2你用fd 2替换了fd 1.

所以,当你再运行5>&1要重定向FD 5到FD 1点,现在(不是它在哪里它启动时).

您需要反转两个重定向:

bash ./one.bash 5>&1 1>&2
Run Code Online (Sandbox Code Playgroud)