在没有EOF信号的情况下将多个命令传递给单个命令 [英] Pipe multiple commands to a single command with no EOF signal wait
问题描述
如何将多个命令的标准输出传送给单个命令?例如:
How can I pipe the std-out of multiple commands to a single command? Something like:
(cat my_program/logs/log.*;tail -0f my_program/logs/log.0) | grep "filtered lines"
我想在一个使用管道的命令行上运行以下所有命令并且没有重定向到临时文件(如果可能的话)。有一个小的细微差别,意味着我不能使用括号;我希望最后一条命令是尾部输入,所以我希望在std-in接收到每行之后发生grep - 而不是等待EOF信号。
I want to run all the following commands on a single command-line using pipes and no redirects to a temp file (if possible). There is one small nuance that means I can't use parentheses; I want the last command to be a tail feed so I want the grep to happen after every line is received by the std-in - not wait for EOF signal.
推荐答案
好的,我有一个混乱的解决方案(这是一个脚本 - 可惜这对命令行来说不切实际):
Okay I have a messy solution (it's a script - sadly this would be unpractical for the command line):
#!/bin/bash
ProcessFIFO(){
while [[ -p /tmp/logfifo ]]; do
grep "filtered lines" < /tmp/logfifo
done
}
OnExit(){
rm -f /tmp/logfifo
echo 'temp files deleted'
exit
}
# Create FIFO
mkfifo /tmp/logfifo
trap OnExit SIGINT SIGTERM
ProcessFIFO &
cat log.* > /tmp/logfifo
tail -0f log.0 > /tmp/logfifo
我已经习惯了@itsbruce建议的FIFO方法,我也必须使用while循环来停止grep从EOF信号结束:
I have used to FIFO method suggested by @itsbruce and I've also had to use a while loop to stop the grep from ending at the EOF signal:
while [[ -p /tmp/logfifo ]]
我还包含一个陷阱来删除FIFO:
I have also included a trap to delete the FIFO:
trap OnExit SIGINT SIGTERM
这篇关于在没有EOF信号的情况下将多个命令传递给单个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!