by gizmo686 · 13y ago · 0 comments
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.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.
{ ./prog1; ./prog2; } | sort | uniq -u
sort < foo | uniq -u
not work with named pipes?The advantage to
cat foo | sort | uniq -u
is that it is (marginally) easier to drop in another filter in front of sort. <foo sort | uniq -u
(redirections can be at the beginning too.)Also: sort -u instead of sort|uniq.