简体   繁体   English

如何使用bash将stdin重定向到FIFO

[英]How to redirect stdin to a FIFO with bash

I'm trying to redirect stdin to a FIFO with bash. 我正在尝试使用bash将stdin重定向到FIFO。 This way, I will be able to use this stdin on an other part of the script. 这样,我就可以在脚本的另一部分上使用这个stdin。

However, it doesn't seem to work as I want 但是,它似乎并不像我想的那样工作

script.bash 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"

Here I would expect , when I run ls | ./script.bash 在我运行ls | ./script.bash时,我希望在这里 ls | ./script.bash , to see the output of ls, but it doesn't work (eg the script exits, without outputing anything) ls | ./script.bash ,查看ls的输出,但它不起作用(例如脚本退出,不输出任何内容)

What am I misunderstanding ? 我有什么误会?

I am pretty sure that less need additional -f flag when reading from pipe. 我很确定从管道读取时不需要额外的-f标志。

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

If that does not help I would also recommend to change order between last two lines of your script: 如果这没有帮助,我还建议您更改脚本的最后两行之间的顺序:

#!/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

In general, I avoid the use of /dev/stdin, because I get a lot of surprises from what is exactly /dev/stdin, especially when using redirects. 一般来说,我避免使用/ dev / stdin,因为我从/ dev / stdin中得到了很多惊喜,特别是在使用重定向时。

However, what I think that you're seeing is that less finishes before your terminal is completely started. 但是,我认为您所看到的是在您的终端完全启动之前完成的less When the less ends, so will the terminal and you won't get any output. less结束时,终端也将结束,你将不会获得任何输出。

As an example: 举个例子:

xterm -e ls

will also not really display a terminal. 也不会真正显示终端。

A solution might be tail -f , as in, for example, 解决方案可能是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

because the tail -f remains alive. 因为tail -f仍然活着。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM