Say I have two bash scripts (p1.sh
and p2.sh
), representing two programs:
#!/bin/bash
echo "hello"
sleep 10s
echo "good morning"
sleep 10s
echo "goodbye !!"
And p2.sh
:
#!/bin/bash
# listen to the output of p1's program
# if p1 gives "goodbye !!" output, echo "p1 said goodbye!"
The workflow is:
- Run
p1.sh
- Get the pid of the
p1
program - Let
p2.sh
listen to thep1
process's output (instead of callingp1.sh
inside ofp2.sh
), and judgep1
's output.
How to realize it? Thanks!
CodePudding user response:
You can include either of the below lines in p2.sh. These command will exit to the next line only when the desired outcome in p1.sh is reached.
If your intent is to check the content of the log, please try below
tail -f <p1_log> |grep -m1 'goodbye'
This command will exit when p1_log displays goodbye. This needs p2.sh started before p1.sh because p2.sh follows/watches p1.sh's log.
If your intent to check if p1.sh has completed in p2.sh, you can use the
tail
command as belowtail --pid=<p1_pid> -f <p1_log>
This command will exit when p1 ends.
Happy to be corrected and learn.
CodePudding user response:
Pipe the output of p1.sh
to a while read
loop.
p1.sh | while read -r line; do
if [ "$line" = "goodby !!" ]
then echo "p1 said goodbye"
fi
done