2

I would like to feed a command with standard input and dump that standard input into the file at the same time. This is just failed attempt:

read | tee dump.txt

this command is waiting for standard input. I expect whatever I input to be fed to read AND to be dumped into dump.txt. How can I do it?

Pablo
  • 2,537
  • 1
    What exactly do you want to achieve? Piping into a plain read is not really giving you any benefits. Anyway, what about just swapping the pipe around? tee dump.txt | read – Byte Commander Sep 01 '17 at 22:01
  • read is an example of some other command, just to show a command with standard input. In reality instead of read it's lpr command which is taking raw input from std and printing it. What I need is to tap that communication and dump input into file as well. – Pablo Sep 01 '17 at 22:08
  • swapping the pipe is almost doing what I want, except that I need an additional newline in order to finish input. Which is not good. – Pablo Sep 01 '17 at 22:11
  • The additional newline is needed because of read. Try with e.g. cat instead, which will read until you close the stream by pressing Ctrl+D (or simply when the piped output ends). – Byte Commander Sep 01 '17 at 22:19
  • but read alone doesn't need double newlines... – Pablo Sep 01 '17 at 22:22
  • I can't really explain to you why it behaves like that (interesting question though), but it's a clear observation, I think. Asked a new question about that here, maybe it will be answered. So does it work that way with your real command? – Byte Commander Sep 01 '17 at 22:38

1 Answers1

0

Swap the pipe around: first get the output through tee to save a copy in a file while also replicating it on its standard output again, then pipe that into your actual command.

I use cat here instead because it takes indefinite amounts of input and doesn't quit after the first line (plus additional newline, see here for an explanation of that) like read:

echo something | tee dump.txt | cat
Byte Commander
  • 107,489