gpt4 book ai didi

linux - bash `read -t` 在管道上不起作用

转载 作者:太空狗 更新时间:2023-10-29 12:25:35 28 4
gpt4 key购买 nike

演示这一点的一个非常简单的方法是运行

mkfifo /tmp/a
read -t 1 a < /tmp/a

读取永远不会返回。

Bash Manual says: This option is only effective if read is reading input from a terminal, pipe, or other special file; it has no effect when reading from regular files

但是/tmp/a 是一个管道,ls 的输出是

ls -l /tmp/a
prw-r--r-- 1 root root 0 Feb 4 22:18 /tmp/a

bash 版本是:

GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2013 Free Software Foundation, Inc.

操作系统是:

Ubuntu 16.04.1 LTS

最佳答案

请参阅我在 unix.stackexchange.com 上的详细回答.

长话短说:你的 -t标志似乎不起作用,因为 read甚至没有执行,因为这是你的 shell 被阻止,而不是你的命令。


在执行你的 read 之前命令,bash试图打开 /tmp/a这是一个阻塞操作。打开命名管道进行读取 block ,直到其他人打开它进行写入。

你可以用错误的命令来检查:

mkfifo my_fifo
a_command_that_does_not_exist < my_fifo

(你的 shell 被阻塞,直到有人打开 my_fifo 进行写入,然后它才会告诉你 command not found )

解决方案:read -t 1 a <> /tmp/a

(更多关于 unix.stackexchange.com )

关于linux - bash `read -t` 在管道上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42041778/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com