bash 无法写入命名管道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15376562/
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-18 04:49:56  来源:igfitidea点击:

Can't write to named pipe

linuxbashnamed-pipes

提问by tay10r

I'm trying to write to a named pipe, made with mkfifo. But when I run the command, (ex) ls > myNamedPipe, I can no longer enter commands into the bash. I can still write characters and that's pretty much it.

我正在尝试写入使用mkfifo制作的命名管道。但是当我运行命令 (ex) 时ls > myNamedPipe,我无法再将命令输入到 bash 中。我仍然可以写字符,仅此而已。

回答by michaelmeyer

A named pipe remains opened until you read it from some other place. This is to permit communication between different processes.

命名管道保持打开状态,直到您从其他地方读取它。这是为了允许不同进程之间的通信。

Try:

尝试:

mkfifo fifo
echo "foo" > fifo

Then open another terminal and type:

然后打开另一个终端并输入:

cat fifo

If you return to you first terminal, you'll notice that you can now enter other commands.

如果您返回到第一个终端,您会注意到现在可以输入其他命令。

See also what happends with the reverse :

另请参阅相反的情况:

# terminal 1
cat fifo

# terminal 2
echo "foo" > fifo

# and now you can see "foo" on terminal 1

If you want you terminal not to "hang on" when trying to write something to the fifo, attach to the fifo a file descriptor :

如果您希望终端在尝试向 fifo 写入内容时不要“挂起”,请将文件描述符附加到 fifo:

mkfifo fifo
exec 3<> fifo
echo "foo" > fifo
echo "bar" > fifo