[英]How do I redirect stdout from one process to be read as file descriptor 3 in another?
I want to redirect (stdout,stderr) from one process to (stdin,file descriptor 3) of another. 我想将(stdout,stderr)从一个进程重定向到另一进程的(stdin,文件描述符3)。 How do I do that?
我怎么做?
For example, if I have myscript.sh
: 例如,如果我有
myscript.sh
:
#!/bin/sh
# myscript.sh
echo "stdout"
echo "stderr" >&2
And reader.sh
: 和
reader.sh
:
#!/bin/sh
# reader.sh
read key
echo "stdin: $key"
read key <&3
echo "fd3: $key"
How do I pipe the output of myscript.sh
to reader.sh
such that the final output is: 我如何将
myscript.sh
的输出通过管道reader.sh
到reader.sh
这样最终输出为:
stdin: stdout
fd3: stderr
The closest I've gotten is: 我得到的最接近的是:
./myscript.sh 2>&3 | ./reader.sh
But that hangs waiting for input (from fd 3) 但这挂起等待输入(来自fd 3)
I would do this with a named pipe: 我将使用命名管道来执行此操作:
mkfifo err
./myscript.sh 2>err | ./reader.sh 3<err
Doing 2>&3 in your attempt just sends stderr to wherever the parent shell has 3 pointed, and will fail if you haven't already opened 3 in that shell. 尝试执行2>&3只会将stderr发送到父shell指向3的任何位置,如果您尚未在该shell中打开3,它将失败。 It doesn't do any good for the reader shell's 3;
这对阅读器外壳的3并没有任何好处。 even if inherited, it would be as a write fd, not a read one.
即使是继承的,也将是写fd,而不是读fd。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.