4 ms·
I don't think named pipes work particuarly well in shell scripts. A program writing to a pipe will not terminate until the output is consumed. However, consumin
by gizmo686 13y ago
I don't think named pipes work particuarly well in shell scripts. A program writing to a pipe will not terminate until the output is consumed. However, consuming the output will not happen until the program finishes and the script arrives that the program that reads from the pipe. Similarly, a program reading from the pipe will not terminate until the program writing to it does. If you start reading before you start writing, then you simply wait. Anonymous pipes solves this problem by launching all of the programs at the same time. You could work around this by telling the shell to run the command in the background (append & in bash).
Having said that, I have had occasion to use named pipes in a shell script. Basically, I had two programs and I wanted to determine which lines were in one, but not both, of their outputs. Using named pipes, I was able to do something like:
mkfifo foo
./prog1 > foo &
./prog2 > foo &
cat foo | sort | uniq -u
Effectively, this provides a way to combine the output of multiple commands. I took it as a matter of faith that doing this won't interleave lines. I assume that as long as both programs flush only at line breaks then there is not any problem.
- derleth 13y agoDoes: sort < foo | uniq -u not work with named pipes?
- dllthomas 13y agoIt works fine. The advantage to cat foo | sort | uniq -u is that it is (marginally) easier to drop in another filter in front of sort.
- knz42 13y agoThis argument is less strong in the face of: <foo sort | uniq -u (redirections can be at the beginning too.) Also: sort -u instead of sort|uniq.
- dllthomas 13y agoIt's still a reason to disprefer the form that was given. I don't see any reason to prefer the cat version over yours, except habits, though. That said, there's only weak reason to avoid the cat.
- jl6 13y agoIt is also simpler, in the sense that it presents a left-to-right order that visually illustrates the pipeline, without the incongruous left-facing angle bracket.
- dllthomas 13y agoYou don't need named pipes for this: { ./prog1; ./prog2; } | sort | uniq -u
- bcoates 13y agoAgreed, every time I've used a named pipe in a shellscript it's caused me nothing but pain. IIRC, they're guaranteed not to interleave write()s less than PIPE_BUF (4k?) but if your program is buffering internally it might not end writes on a newline.