“阅读”在 bash 中从管道读取时不会超时
我使用 Now 创建一个管道
mkfifo /tmp/foo.pipe
,我想尝试从管道中读取最多 2 秒,因此我执行
read -t 2 line < /tmp/foo.pipe
时不会发生超时。 Read 只是坐在那里等待来自管道的输入。
手册说“读取”应该与命名管道一起使用。有谁知道为什么会发生这种情况?
ls -al /tmp/foo.pipe
prw-r----- 1 foo bar 0 Jun 22 19:06 /tmp/foo.pipe
I create a pipe using
mkfifo /tmp/foo.pipe
Now, I want to try reading from the pipe for a maximum of 2 seconds, so I execute
read -t 2 line < /tmp/foo.pipe
The timeout does not occur. Read just sits there waiting for input from the pipe.
The manuals say that 'read' is supposed to work with named pipes. Does anyone have an idea why this is happening?
ls -al /tmp/foo.pipe
prw-r----- 1 foo bar 0 Jun 22 19:06 /tmp/foo.pipe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在调用 read 内置函数之前,您的 shell 会阻塞 open() 调用。
在 Linux 上,您可以同时打开 FIFO 进行读取和写入,以防止打开时发生阻塞;这是不可移植的,但可以做你想做的。
改编自:具有非阻塞读取的 Bash 脚本
Your shell is blocking on the open() call before invoking the read builtin.
On Linux, you can open the FIFO for both read and write at the same time to prevent blocking on open; this is non-portable, but may do what you want.
Adapted from: Bash script with non-blocking read
如果您只想刷新(并丢弃) FIFO 中的数据:
If you just want to flush (and discard) the data from the FIFO:
您的 shell 是支撑它的那个 shell,它正在尝试从管道读取数据以将数据输入到读取命令中,并且由于它没有获取任何内容,因此它只是坐在那里等待。
Your shell is the one that is holding it up, it is attempting to read from the pipe to feed the data into the read command, and since it is not getting anything it just sits there waiting.