bash - 从 bash 中的管道读取时“读取”不会超时

标签 bash timeout

我使用

创建了一个管道
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

最佳答案

在调用 read 内置函数之前,您的 shell 会阻塞 open() 调用。

在Linux上,可以同时打开FIFO进行读写,防止打开时阻塞;这是不可移植的,但可以做你想做的事。

read -t 2 <>/tmp/foo.pipe

改编自:Bash script with non-blocking read

关于bash - 从 bash 中的管道读取时“读取”不会超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6448632/

相关文章:

linux - 如何在 bash 脚本命令中提供 2 个输入?

linux - 如何在此脚本中硬编码密码

ruby-on-rails - Development.log 日志文件不记录 Rails SQL 查询

php - 最大执行时间超过 60 秒错误

timeout - 是否可以编写脚本来避免VPN超时

amazon-web-services - AWS SQS : Which is the order of message in a FIFO queue after visibility-timeout expires?

linux - unix/linux 划分两个不同文件包含的行数的方法是什么?

c++ - Bash 脚本,如果 C++ 返回一些东西

java - 用于自定义超时设置的 java.net.URL 的替代方案

c++ - 更改 odbc 超时