如何使用 bash 冲洗管道

发布于 2024-09-11 12:03:10 字数 79 浏览 3 评论 0原文

我有一个写入命名管道的脚本和另一个从管道读取的脚本。有时,在启动脚本时,我注意到管道的内容在脚本的先前运行中存在。有没有办法在脚本开头冲洗管道?

I have a script that writes to a named pipe and another that reads from the pipe. Occasionally, when starting the script I have noticed that the contents of the pipe exist from a previous run of the script. Is there a way to flush out the pipe at the beginning of the script?

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

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

发布评论

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

评论(3

赠意 2024-09-18 12:03:10

我认为 dd 是你的朋友:

dd if=myfifo iflag=nonblock of=/dev/null

strace 显示

open("myfifo", O_RDONLY|O_NONBLOCK)

并且实际上甚至不会阻塞空 fifo。

I think dd is your friend:

dd if=myfifo iflag=nonblock of=/dev/null

strace shows

open("myfifo", O_RDONLY|O_NONBLOCK)

and indeed doesn't even block on an empty fifo.

摘星┃星的人 2024-09-18 12:03:10

您可以从管道中读取数据,直到它为空。这将有效地冲洗它。

在尝试这一大胆的壮举之前,请调用 fcntl(mypipe, F_SETFL, O_NONBLOCK) (我不知道 shell 脚本等效项)在管道为空时进行读取,而不是挂起程序。

You can read from the pipe until it is empty. This will effectively flush it.

Before you attempt this daring feat, call fcntl(mypipe, F_SETFL, O_NONBLOCK) (I don't know the shell-scripting equivalent) to make a read when the pipe is empty not hang your program.

冷了相思 2024-09-18 12:03:10

试试这个:

“设置管道时打开 FD 读/写而不是只读可以防止阻塞。”

来自:

设置从命名管道读取的管道,无需在 bash 中阻塞

Try this:

"Opening the FD read/write rather than read-only when setting up the pipeline prevents blocking."

from:

Setting up pipelines reading from named pipes without blocking in bash

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