简体   繁体   English

如何将stdout从一个进程重定向到另一个进程中的文件描述符3中读取?

[英]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.shreader.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.

相关问题 将stdout传递到另一个进程的文件描述符 - Pipe stdout to another process's file descriptor 除了读/回声之外,还有没有其他方法可以在不关闭fd的情况下从文件描述符读取一行到stdout? - is there a way other than read/echo to read one line from a file descriptor to stdout without closing the fd? 如何使用Expect将stdout从程序连续重定向到生成的进程中? - How do I redirect stdout from a program continuously into a spawned process using expect? 如何从unix系统中的另一个进程关闭文件描述符 - How to close a file descriptor from another process in unix systems 从bash脚本中将stdout和stderr的副本重定向到一个文件,将stderr的副本重定向到另一个文件 - Redirect copy of stdout and stderr to one file, copy of just stderr to another file from within bash script 将stdout和stderr重定向到一个文件,将stderr的副本重定向到另一个文件 - redirect stdout and stderr to one file, copy of just stderr to another 如何将标准输出文件描述符放入变量中? - How to put stdout file descriptor in a variable? Bash:如何将一个进程的stdout和stderr传递给两个不同的进程? - Bash: how do I pipe stdout and stderr of one process to two different processes? 如何在bash脚本中使用文件描述符3中的“read”读取? - How to read using “read” from file descriptor 3 in bash script? 如何从bash中的子进程实时读取标准输出 - How to read stdout from a sub process in bash in real time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM