1つのプログラムの出力を他の2つのプログラムにパイプする方法はありますか?


28

これはばかげた質問ですが、私はこのようなことを1行で達成しようとしています:

$ prog1 | prog2
$ prog1 | prog3

したがって、基本的にprog1を実行し、出力をprog2とprog3に別々にパイプします(チェーンパイプではありません)。最初は、teeを使おうとしていましたが、出力をファイルにダンプしていましたので、正しくありませんでした(これは望んでいないことです)。

$ prog1 | tee prog2 | prog3 # doesn't work - creates file "prog2"

ある時点で、これを出力を2つ以上のプログラムにパイプするように拡張したいと思うかもしれませんが、今は単純に始めています。

$ prog1 | prog2
$ prog1 | prog3
$ prog1 | prog4
...

zshでこれができると思います。
キース

回答:



16

Ignacioの答えと同様に、を使用して一時的な名前付きパイプを使用できますmkfifo(1)

mkfifo /tmp/teedoff.$$; cmd | tee /tmp/teedoff.$$ | prog2 & sleep 1; prog3 < /tmp/teedoff.$$; rm /tmp/teedoff.$$

これはもう少し冗長ですが、のようなプロセス置換がないシステムで動作しますdashsleep 1任意の競合状態を処理することです。


6

仕事をする小さなユーティリティpteeがあります:

prog1 | ptee 2 3 4 2> >(prog2) 3> >(prog3) 4> >(prog4)

ファイルに書き込む代わりに、pteeはコマンドラインで指定されたすべてのfdsに書き込みます。

PTEEはの一部であるpipexec


4

bashismや特別なファイルなどは必要ありません-とにかくLinuxにはありません:

% { prog1 | tee /dev/fd/3 | prog2 >&2 ; } 3>&1 | prog3 

{ { printf %s\\t%s\\t%s\\n \
    "this uneven argument list" \
    "will wrap around" to \
    "different combinations" \
    "for each line." "Ill pick out" \
    "a few words" "and grep for them from" \
    "the same stream." | 
 tee /dev/fd/3 /dev/fd/4 | 
 grep combination >&2 ; } 3>&1 |
 grep pick >&2 ; } 4>&1 | 
 grep line

different combinations  for each *line.*  Ill pick out
different combinations  for each line.  Ill *pick* out
different *combinations*  for each line.  Ill pick out

grep強調表示された結果にスターを付けて、同じストリームからの3つの結果であるだけでなく、別のgrepプロセス一致の結果であることも示しました。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.