Reputation: 3855
I run a program which writes a lot to 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).
How can I do this in bash?
A bit more explained: I need to run an installation program which is an executable from a mounted dvd1.iso. After some time it prints "Info: Eject DVD 1 and insert DVD 2 to continue.". And this is what shall be done automatically.
Following the answer here my test set up:
talker.sh
#!/bin/bash
for VAR in {1..20}
do
sleep 1s
echo "huhu $VAR"
done
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
...
Upvotes: 3
Views: 594
Reputation: 7539
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. 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.
Upvotes: 4