6

I'd like something in the form of (fill in the blanks):

cmd1 | ____ cmd2 ____

That was equivalent to this, but without writing any files:

cmd1 > temp
cmd2 < temp | diff temp -

cmd1 isn't actually a command, it's a stdin stream from a text editor, so it cannot be run more than once, so this won't work:

diff <(cmd1) <(cmd1 | cmd2)

But that is essentially what I want.

I'm using macOS, but I'd prefer it linux compatible as well.

5

1 Answer 1

6

Generally when you do not want to write a file, you can replace it by a (named) pipe, but a pipe cannot be read by two processes simultaneously. In your case you can use tee to split your input and use a named pipe for one of its ends:

mkfifo p
cmd1 | tee p | cmd2 | diff p -
rm p

This, however, has one major drawback: the pipes have a limited buffer (64k on Linux), so when diff would read one of them excessively comparing to the other, the whole pipeline will be deadlocked. The problem can be circumvented using the buffer command for at least one pipe:

mkfifo p
cmd1 | tee p | cmd2 | buffer | diff p -
rm p

See also here for other buffering options.

Another solution for deadlocks is to use a variable to buffer the whole cmd1 output in it (loading it to the memory):

cmd1 | ( b=$(cat); cmd2 <<<"$b" | diff <(echo "$b") - )
6
  • Thank you, I thought it might involve pipes. That said, because this is a stream from a text editor, it does need to be fully encapsulated in one command ideally. How might you extract the cmd1 | to be outside of the mkfifo? I don't know how to pipe direct stdin to the 2nd of a semi colon delimited chain of commands, in other words. Jan 13, 2017 at 17:04
  • Also, it seems buffer is not standard (at least not on osx) so I probably can't use that. It's starting to feel like I should just use temporary emacs buffers for this instead, but then that begs the question on how I might send 2 emacs buffers at once to diff... Jan 13, 2017 at 17:07
  • 1
    Because mkfifo does not read stdin, you may just write mkfifo p; tee p ... or create the fifo once, and not to bother with it here.As for the buffer, did you try other options under the link?
    – mik
    Jan 13, 2017 at 18:01
  • Ah, I didn't know that, thanks. re: buffer, macOS lacks pv, buffer and mbuffer all. Jan 13, 2017 at 18:56
  • @AaronJensen At least pv is available through HomeBrew.
    – Kusalananda
    Jan 16, 2017 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .