如何在Bash中加入视频流? [英] How do I prepend to a stream in Bash?
问题描述
假设我在bash中具有以下命令:
Suppose I have the following command in bash:
one | two
one
长时间运行会产生输出流,而two
在该流的每一行上都执行快速操作,但是two
根本不起作用,除非它读取的第一个值告诉它如何每行要读取的值很多. one
不会输出该值,但我事先知道它的值(假设它是15
).我想在one
的输出之前通过管道发送15\n
.我不想修改one
或two
.
one
runs for a long time producing a stream of output and two
performs a quick operation on each line of that stream, but two
doesn't work at all unless the first value it reads tells it how many values to read per line. one
does not output that value, but I know what it is in advance (let's say it's 15
). I want to send a 15\n
through the pipe before the output of one
. I do not want to modify one
or two
.
我首先想到的是:
echo "$(echo 15; one)" | two
这给了我正确的输出,但是直到命令one
完成时它才完全不流经管道.我希望输出立即开始通过管道流式传输,因为执行需要很长时间(数月).
That gives me the correct output, but it doesn't stream through the pipe at all until the command one
finishes. I want the output to start streaming right away through the pipe, since it takes a long time to execute (months).
我也尝试过:
echo 15; one | two
当然会输出15,但不会通过管道将其发送到two
.
Which, of course, outputs 15, but doesn't send it through the pipe to two
.
bash中是否有办法通过管道传递"15 \ n",然后开始通过同一管道流式传输one
的输出?
Is there a way in bash to pass '15\n' through the pipe and then start streaming the output of one
through the same pipe?
推荐答案
您只需要shell分组构造:
You just need the shell grouping construct:
{ echo 15; one; } | two
需要在括号和尾部分号周围留空格.
The spaces around the braces and the trailing semicolon are required.
要测试:
one() { sleep 5; echo done; }
two() { while read line; do date "+%T - $line"; done; }
{ printf "%s\n" 1 2 3; one; } | two
16:29:53 - 1
16:29:53 - 2
16:29:53 - 3
16:29:58 - done
这篇关于如何在Bash中加入视频流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!