[英]bash: trigger output on stdout
I run a program which writes a lot to stdout
. 我运行了一个向stdout
写很多东西的程序。 After a certain time this program prints a defined line to stdout
and I need to trigger this in order to call a function parallel (without terminating the first program). 一定时间后,该程序将已定义的行打印到stdout
,我需要触发该行才能并行调用一个函数(不终止第一个程序)。
How can I do this in bash? 我该如何在bash中做到这一点?
A bit more explained: I need to run an installation program which is an executable from a mounted dvd1.iso . 进一步说明:我需要运行安装程序,该程序是已安装的dvd1.iso的可执行文件。 After some time it prints "Info: Eject DVD 1 and insert DVD 2 to continue.". 一段时间后,它将打印“信息:弹出DVD 1并插入DVD 2以继续。”。 And this is what shall be done automatically. 这将是自动完成的。
Following the answer here my test set up: 按照这里的答案我的测试设置:
talker.sh talker.sh
#!/bin/bash
for VAR in {1..20}
do
sleep 1s
echo "huhu $VAR"
done
listener.sh listener.sh
#!/bin/bash
bash talker.sh \
| tee output.txt \
| grep --line-buffered "huhu 3" \
| ( while read -r line; do echo "found"; done; ) &\
tail -f output.txt
and how it works: 以及它是如何工作的:
$ bash listerner.sh
huhu 1
huhu 2
huhu 3
found
huhu 4
huhu 5
...
You could save the output of the first program in a file with tee
and, at the same time, filter the output to get the desired pattern. 您可以将第一个程序的输出保存在带有tee
的文件中,同时过滤输出以获得所需的模式。 Something like: 就像是:
./program | tee output.txt | grep --line-buffered pattern | ( while read -r line; do something; done; )
As suggested in a comment below by @thatotherguy the option --line-buffered
should prevent grep
to hold on to the matches. 正如@thatotherguy在下面的注释中所建议的那样,--line --line-buffered
选项应防止grep
继续进行匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.