可能重复:
osx/linux:管道进入两个进程?
有没有一种方法可以将一个命令的输出通过管道传输到另外两个命令的输入中,同时运行它们?
类似这样的:
$ echo 'test' |(cat) |(cat)
test
test
我想这样做的原因是我有一个程序,它从 USB SDR 设备接收 FM 无线电信号,并将音频输出为原始 PCM 数据(类似于 .wav 文件,但没有标头。)因为信号是不是音乐,而是 POCSAG 寻呼机数据,我需要将其通过管道传输到解码器程序以恢复寻呼机文本。不过,我也想听信号,这样我就知道是否有数据传入。 (否则我无法判断解码器是否损坏或没有数据被广播。)因此,除了将数据通过管道传输到寻呼机解码器之外,我还需要将相同的数据通过管道传输到
play
命令。
目前我只知道如何做一个 - 要么将其传输到解码器并默默地读取数据,要么将其传输到
play
并听到它而不看到任何解码的文本。
如何将相同的数据传输到两个命令,以便我可以阅读文本并听到音频?
我无法使用
tee
,因为它只将重复的数据写入文件,但我需要实时处理数据。
如果同时使用
tee
和mkfifo
应该没问题。
mkfifo pipe
cat pipe | (command 1) &
echo 'test' | tee pipe | (command 2)
>(command)
语法:echo "Hello world." | tee >(sed 's/^/1st: /') >(sed 's/^/2nd cmd: /') >/dev/null
可能会回来:
2nd cmd: Hello world.
1st: Hello world.
somefile.ext
,保存它们,计算md5sum和sha1sum:wget -O - http://somewhere.someland/somepath/somefile.ext |
tee somefile.ext >(md5sum >somefile.md5) | sha1sum >somefile.sha1
或
wget -O - http://somewhere.someland/somepath/somefile.ext |
tee >(md5sum >somefile.md5) >(sha1sum >somefile.sha1) >somefile.ext
有一种方法可以通过无名管道来做到这一点(在linux下测试):
(( echo "hello" |
tee /dev/fd/5 |
sed 's/^/1st occure: /' >/dev/fd/4
) 5>&1 |
sed 's/^/2nd command: /'
) 4>&1
给:
2nd command: hello
1st occure: hello
还有:
echo "hello" | (
( tee /dev/fd/5 |
sed 's/^/1st occure: /' >/dev/fd/4
) 5>&1 |
sed 's/^/2nd command: /'
) 4>&1
此示例将让您下载
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
或
wget -O - http://somewhere.someland/somepath/somefile.ext |
(( tee /dev/fd/5 | md5sum >/dev/fd/4 ) 5>&1 | tee somefile.ext | sha1sum ) 4>&1
也许看看
tee
命令。它所做的只是将其输入打印到文件中,但也将其输入打印到标准输出。所以类似:
echo "Hello" | tee try.txt | <some_command>
将创建一个内容为“Hello”的文件,并让“Hello”(流经管道)最终作为
<some_command>
的 STDIN。