如何使用 bash 将标准输入重定向到 FIFO

How to redirect stdin to a FIFO with bash

我正在尝试使用 bash 将标准输入重定向到 FIFO。这样,我就可以在脚本的其他部分使用这个标准输入了。

然而,它似乎并没有如我所愿

script.bash

#!/bin/bash

rm /tmp/in -f
mkfifo /tmp/in
cat >/tmp/in &

# I want to be able to reuse /tmp/in from an other process, for example : 
xfce4-terminal --hide-menubar --title myotherterm --fullscreen -x bash -i -c "less /tmp/in"

这里我希望,当我 运行 ls | ./script.bash 时看到 ls 的输出,但是它不起作用(例如脚本退出,没有输出任何东西)

我误会了什么?

一般来说,我避免使用 /dev/stdin,因为我从 /dev/stdin 中得到很多惊喜,尤其是在使用重定向时。

但是,我认为您看到的是 less 在您的终端完全启动之前完成。当 less 结束时,终端也将结束,您不会得到任何输出。

举个例子:

xterm -e ls

也不会真正显示终端。

解决方案可能是 tail -f,例如

#!/bin/bash

rm -f /tmp/in
mkfifo /tmp/in
xterm -e "tail -f /tmp/in" &

while :; do
    date > /tmp/in
    sleep 1
done

因为 tail -f 还活着。

我很确定从管道读取时 less 需要额外的 -f 标志。

test_pipe is not a regular file (use -f to see it)

如果这没有帮助,我还建议更改脚本最后两行之间的顺序:

#!/bin/bash

rm /tmp/in -f
mkfifo /tmp/in

xfce4-terminal --hide-menubar --title myotherterm --fullscreen -x bash -i -c "less -f /tmp/in" &

cat /dev/stdin >/tmp/in