edi9999
edi9999

Reputation: 20544

How to redirect stdin to a FIFO with bash

I'm trying to redirect stdin to a FIFO with bash. This way, I will be able to use this stdin on an other part of the script.

However, it doesn't seem to work as I want

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, to see the output of ls, but it doesn't work (eg the script exits, without outputing anything)

What am I misunderstanding ?

Upvotes: 4

Views: 4770

Answers (2)

Kamil
Kamil

Reputation: 36

I am pretty sure that less need additional -f flag when reading from pipe.

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

Upvotes: 1

Ljm Dullaart
Ljm Dullaart

Reputation: 4969

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.

However, what I think that you're seeing is that less finishes before your terminal is completely started. When the less ends, so will the terminal and you won't get any output.

As an example:

xterm -e ls

will also not really display a terminal.

A solution might be tail -f, as in, for example,

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

Upvotes: 0

Related Questions