Size: 448
Comment: uh ? what is this faq about
|
Size: 766
Comment: thanks to the one who submitted the fd solution, it's great.
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
== How do I prevent a named pipe from closing? == | == How to write several times to a fifo without having to reopen it? == |
Line 4: | Line 4: |
We've found two ways so far: | 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? |
Line 6: | Line 11: |
* Using a loop {{{ while cat myfifo; do :; done}}} |
If the commands are consecutive, they can be grouped inside a subshell and redirect it's output: {{{ cat < myfifo & (echo 'a'; echo 'b'; echo 'c';) > myfifo }}} |
Line 10: | Line 17: |
* Using tail {{{ tail -n +1 -f 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 & |
Line 14: | Line 21: |
What problem are you trying to solve? Open a program that reads the pipe and send the output of several commands to it? like: {{{ mkfifo myfifo cat < myfifo & |
# assigning fd 3 to the pipe |
Line 21: | Line 23: |
echo blah >&3 echo more blah >&3 |
# writing to fd 3 instead of the pipe echo 'a' >&3 echo 'b' >&3 echo 'c' >&3 # closing the fd |
Line 24: | Line 31: |
}}} | }}} |
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 inside a subshell 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>&-