bash 管道输出到两个不同的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13107783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:53:36  来源:igfitidea点击:

Pipe output to two different commands

bashpipetee

提问by Malvineous

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

可能的重复:
osx/linux:管道进入两个进程?

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 playcommand.

我想这样做的原因是我有一个程序可以从 USB SDR 设备接收 FM 无线电信号,并将音频输出为原始 PCM 数据(如 .wav 文件但没有标题。)因为信号不是音乐但 POCSAG 寻呼机数据,我需要将其通过管道传输到解码器程序以恢复寻呼机文本。但是我也想听信号,所以我知道是否有任何数据进入。(否则我无法判断解码器是否损坏或只是没有数据正在广播。)因此,除了将数据传送到寻呼机解码器之外,我还需要将相同的数据传送到play命令。

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

目前我只知道如何做 - 要么将其传输到解码器并静默读取数据,要么将其传输到play并在没有看到任何解码文本的情况下听到它。

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

如何将相同的数据通过管道传输到两个命令,以便我可以阅读文本并听到音频?

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

我不能使用tee,因为它只将重复的数据写入文件,但我需要实时处理数据。

回答by Tim Green

It should be ok if you use both teeand mkfifo.

如果同时使用teeand 应该没问题mkfifo

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

回答by F. Hauri

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

有一种方法可以通过未命名管道(在 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:

此示例将让您下载somefile.ext、保存它们、计算他的 md5sum 并计算他的 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

回答by Ivaylo Strandjev

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

也许看看tee命令。它所做的只是将其输入打印到文件中,但它也将其输入打印到标准输出中。所以像:

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

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

将创建一个内容为“Hello”的文件,并将 hello 作为参数传递给<some_command>.