By Malvineous


2012-10-28 09:45:02 8 Comments

Possible Duplicate:
osx/linux: pipes into two processes?

Is there a way to pipe the output from one command into the input of two other commands, running them simultaneously?

Something like this:

$ echo 'test' |(cat) |(cat)
test
test

The reason I want to do this is that I have a program which receives an FM radio signal from a USB SDR device, and outputs the audio as raw PCM data (like a .wav file but with no header.) Since the signal is not music but POCSAG pager data, I need to pipe it to a decoder program to recover the pager text. However I also want to listen to the signal so I know whether any data is coming in or not. (Otherwise I can't tell if the decoder is broken or there's just no data being broadcast.) So as well as piping the data to the pager decoder, I also need to pipe the same data to the play command.

Currently I only know how to do one - either pipe it to the decoder and read the data in silence, or pipe it to play and hear it without seeing any decoded text.

How can I pipe the same data to both commands, so I can read the text and hear the audio?

I can't use tee as it only writes the duplicated data to a file, but I need to process the data in real-time.

3 comments

@F. Hauri 2012-10-28 10:48:55

There is a way to do that via unnamed pipe (tested under linux):

 (( echo "hello" |
         tee /dev/fd/5 |
             sed 's/^/1st occure: /' >/dev/fd/4
    ) 5>&1 |
    sed 's/^/2nd command: /'
 ) 4>&1

give:

2nd command: hello
1st occure: hello

This sample will let you download somefile.ext, save them, compute his md5sum and compute his sha1sum:

(( wget -O - http://somewhere.someland/somepath/somefile.ext |
    tee /dev/fd/5 |
    md5sum >/dev/fd/4
  ) 5>&1 |
  tee somefile.ext |
  sha1sum
) 4>&1

@F. Hauri 2015-04-23 08:52:39

@Tim Green 2012-10-28 09:51:11

It should be ok if you use both tee and mkfifo.

mkfifo pipe
cat pipe | (command 1) &
echo 'test' | tee pipe | (command 2)

@Evi1M4chine 2013-12-10 14:14:35

This is very useful to save IO when e.g. making backups. One can pipe the tar output through pv and through sha512sum before writing it, avoiding double or even triple reads/writes to/from disk.

@Evi1M4chine 2013-12-10 17:25:57

by the way, since this can get very confusing, if you want to fail on a certain command, you have to write { somecommand || errorhandler; } | tee pipe | command2, or even { somecommand || errorhandlerforcommand1; } | tee pipe | command2 || errorhandlerforcommand2, as both somecommand || errorhandler | tee pipe | command2 and somecommand | tee pipe | command2 || errorhandler are not handling errors of somecommand as one might expect!

@Evi1M4chine 2013-12-10 17:29:41

Finally, to pipe just stderr into e.g. a log handler, you must add 2> >(errlogger) before the || as so: { somecommand 2> >(errlogger) || errorhandlerforcommand1; } | tee pipe | command2. Everything else will explode in your face.

@izomorphius Support Monica 2012-10-28 09:46:34

Maybe take a look at tee command. What it does is simply print its input to a file, but it also prints its input to the standard output. So something like:

echo "Hello" | tee try.txt | <some_command>

Will create a file with content "Hello" AND will pass hello as argument to <some_command>.

@cnicutar 2012-10-28 09:48:05

I can't use tee as it only writes the duplicated data to a file, but I need to process the data in real-time.

@izomorphius Support Monica 2012-10-28 09:49:28

Maybe I missunderstood you. Don't you need the raw data to be passed to play?

@Malvineous 2012-10-28 09:51:14

@izomorphius: I need to hear the data "live" - if it is redirected to a file, there will be a delay, so it will no longer be live. Also the file will grow in size until I run out of disk space, if I listen for a long time!

@izomorphius Support Monica 2012-10-28 09:52:17

But you can use tee to redirect the output to a pipe. It does not have to be a file.

@Malvineous 2012-10-28 10:48:32

@izomorphius: Ah yes, but I had forgotten about pipes and that was conveniently missing from your answer :-P

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Difference between sh and bash

  • 2011-04-20 03:33:16
  • Weiwei Yang
  • 453498 View
  • 1184 Score
  • 11 Answer
  • Tags:   bash shell unix sh

29 Answered Questions

[SOLVED] How to change the output color of echo in Linux

24 Answered Questions

[SOLVED] Pipe to/from the clipboard in Bash script

31 Answered Questions

[SOLVED] How do I parse command line arguments in Bash?

14 Answered Questions

[SOLVED] How do I set a variable to the output of a command in Bash?

14 Answered Questions

[SOLVED] echo that outputs to stderr

  • 2010-06-07 14:36:16
  • BCS
  • 501532 View
  • 1015 Score
  • 14 Answer
  • Tags:   bash

12 Answered Questions

[SOLVED] How to pipe stderr, and not stdout?

15 Answered Questions

[SOLVED] Pipe output and capture exit status in Bash

4 Answered Questions

[SOLVED] How can I pipe the output of a program that can only write to a file (and not to STDOUT)?

  • 2009-11-22 01:57:00
  • Ryan C. Thompson
  • 551 View
  • 4 Score
  • 4 Answer
  • Tags:   linux pipe

Sponsored Content