[SOLVED] Stop the running Command if a String Is found on stdout
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Maybe I am missing something but I ran the following examples:
Code:
if ping -c3 google.com | grep -q 'time=14.. ms';then
echo true
else
echo false
fi
pgrep ping
if ping google.com | grep -q 'time=14.. ms';then
echo true
else
echo false
fi
pgrep ping
Both times there was no ping command running when pgrep ran. In the first example ping terminates because it has reached is max count (-c3) and false is echo'ed in the console. In the second example grep
terminates and true is echo'ed. The ping command terminates due to a broken pipe condition. So in your example you should not need the if condition at all.
PS:
Your grep is very specific. If your ping is not terminating it is likely because the grep does not find the string. I have a feeling that we are facing an XY problem here. What are you actually trying to do?
In the second example grep terminates and true is echo'ed.
This is strange. Why should grep terminate? It should read until its input reaches end-of-file - which never happens, because ping keeps on generating output.
However I do agree with your assessment that the OP probably asks the wrong question.
This is strange. Why should grep terminate? It should read until its input reaches end-of-file - which never happens, because ping keeps on generating output.
However I do agree with your assessment that the OP probably asks the wrong question.
I assume that would be something like that:
Code:
while ping -c 1 host | grep whatever
do
sleep well
done
and that loop will be stopped based on the result of grep.
This is strange. Why should grep terminate? It should read until its input reaches end-of-file - which never happens, because ping keeps on generating output.
However I do agree with your assessment that the OP probably asks the wrong question.
From the manpage (emphasis by me):
Code:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
It does not make much sense to keep matching results if they are not going to be displayed or written anywhere. But I agree, it is not obvious that grep would behave this way.
@pan64 I actually marked it solved and hit the "yes" button prior to your suggestion. Earlier, I used to thank via a reply as well. However, I stopped doing it because I thought it is not the norm here.
Please be sure of the fact that I can not thank you (plural) enough to spend your precious time helping me solve my problem. All of you who help me owe me big time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.