Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe output to two different commands [duplicate]

Tags:

bash

pipe

tee

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.

like image 414
Malvineous Avatar asked Oct 28 '12 09:10

Malvineous


People also ask

How do you pipe multiple commands?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

Which command can be used to send the output of a command to 2 different places?

"Tee" is explained in the Wikipedia article tee (command). Central is: "The tee command reads standard input, then writes its content to standard output and simultaneously copies it into the specified file(s) or variables.".

Which character is used to pipe output from one command to another?

The pipe character | is used to connect the output from one command to the input of another. > is used to redirect standard output to a file. Try it in the shell-lesson-data/exercise-data/proteins directory!

Can be used to run multiple commands one after the other?

The semicolon (;) operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds.


2 Answers

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

mkfifo pipe cat pipe | (command 1) & echo 'test' | tee pipe | (command 2) 
like image 200
Tim Green Avatar answered Sep 18 '22 04:09

Tim Green


Recent bash present >(command) syntax:

echo "Hello world." | tee >(sed 's/^/1st: /')  >(sed 's/^/2nd cmd: /') >/dev/null 

May return:

2nd cmd: Hello world. 1st: Hello world. 

download somefile.ext, save them, compute md5sum and sha1sum:

wget -O - http://somewhere.someland/somepath/somefile.ext |     tee somefile.ext >(md5sum >somefile.md5) | sha1sum >somefile.sha1 

or

wget -O - http://somewhere.someland/somepath/somefile.ext |     tee >(md5sum >somefile.md5) >(sha1sum >somefile.sha1) >somefile.ext 

Old answer

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 
like image 30
F. Hauri Avatar answered Sep 20 '22 04:09

F. Hauri