News for hackers. Stuff that matters.
Re:Unix: When pipes get names by gizmo686 on Tuesday October 08, 2013 @07:47PM (#6518623)
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.
Re:Unix: When pipes get names by bcoates on Tuesday October 08, 2013 @09:03PM (#6518891)
Agreed, 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.

Re:Unix: When pipes get names by dllthomas on Tuesday October 08, 2013 @08:16PM (#6518741)
You don't need named pipes for this:

{ ./prog1; ./prog2; } | sort | uniq -u

Re:Unix: When pipes get names by derleth on Tuesday October 08, 2013 @08:11PM (#6518717)
Does:

    sort < foo | uniq -u
not work with named pipes?
Re:Unix: When pipes get names by dllthomas on Tuesday October 08, 2013 @08:18PM (#6518749)
It 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.
Re:Unix: When pipes get names by knz42 on Wednesday October 09, 2013 @02:52AM (#6519776)
This 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.

Re:Unix: When pipes get names by dllthomas on Wednesday October 09, 2013 @11:04AM (#6521651)
It'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.
Re:Unix: When pipes get names by jl6 on Wednesday October 09, 2013 @12:42PM (#6522307)
It 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.