Issue
I want to send a SIGKILL if a certain keyword is shown in the output of the command. Eg, if issue the following command:
ubuntu@ip-172-31-24-250:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=109 time=0.687 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=109 time=0.704 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=109 time=0.711 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=109 time=0.809 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=109 time=0.727 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=109 time=0.835 ms
^C
After seeing the output "icmp_seq=6" I'd like the command to automatically halt. I know that ping 8.8.8.8 -c 6 will have the same results, but it's just for the example.
Solution
Would you please try the following:
#!/bin/bash
ping 8.8.8.8 | while IFS= read -r line; do
echo "$line"
[[ $line =~ icmp_seq=6 ]] && kill -9 $(pidof ping)
done
Answered By - tshiono Answer Checked By - Cary Denson (WPSolving Admin)