Differences between revisions 3 and 4
Revision 3 as of 2007-09-11 17:58:41
Size: 766
Editor: irc2samus
Comment: thanks to the one who submitted the fd solution, it's great.
Revision 4 as of 2007-09-12 16:18:48
Size: 750
Editor: irc2samus
Comment: replacing subshell with grouping according to pgas's suggestion
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
If the commands are consecutive, they can be grouped inside a subshell and redirect it's output: If the commands are consecutive, they can be grouped and redirect it's output:
Line 14: Line 14:
(echo 'a'; echo 'b'; echo 'c';) > myfifo { echo 'a'; echo 'b'; echo 'c'; } > myfifo

Anchor(faq85)

How to write several times to a fifo without having to reopen it?

The basic use of named pipes is:

cat < myfifo &
echo 'a' > myfifo

this works but, cat dies after it. What if we want to write several times to the pipe without having to reopen it?

If the commands are consecutive, they can be grouped and redirect it's output:

cat < myfifo &
{ echo 'a'; echo 'b'; echo 'c'; } > myfifo

But if they can't be grouped for some reason, a better way is to assign a file descriptor to the pipe and write there:

cat < myfifo &

# assigning fd 3 to the pipe
exec 3>myfifo

# writing to fd 3 instead of the pipe
echo 'a' >&3
echo 'b' >&3
echo 'c' >&3

# closing the fd
exec 3>&-

BashFAQ/085 (last edited 2016-10-17 20:45:47 by 163-172-21-117)