LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-08-2020, 07:56 AM   #1
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Rep: Reputation: 2
Stop the running Command if a String Is found on stdout


How Can I close ping if a string is found in the output:

Code:
$ ping google.com
PING google.com (74.125.130.138) 56(84) bytes of data.
64 bytes from sb-in-f138.1e100.net (74.125.130.138): icmp_seq=1 ttl=44 time=50.3 ms
64 bytes from sb-in-f138.1e100.net (74.125.130.138): icmp_seq=2 ttl=44 time=49.5 ms
64 bytes from sb-in-f138.1e100.net (74.125.130.138): icmp_seq=3 ttl=44 time=49.5 ms
64 bytes from sb-in-f138.1e100.net (74.125.130.138): icmp_seq=4 ttl=44 time=49.5 ms
Now I want something like:

Code:
if ping google.com | grep -q 'time=49.5'; then
  //terminate ping
fi
So that, if time=49.5 is there it will terminate ping command.
 
Old 06-08-2020, 08:14 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
I would say this is much better to use:
Code:
while <command1>
do
   <command2>
done
 
Old 06-08-2020, 09:27 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Code:
if ping google.com | grep -q 'time=49.5'; then
  pkill ping
fi
Problem: You risk mass-murder instead of a surgical kill (i.e. it may not only kill ping).

EDIT: The above grep will never stop, thus you will never enter the then branch.

Perhaps better:

Code:
ping google.com > /tmp/pingoogle &
PING=$!
while ! grep -q time=49.5 /tmp/pingoogle
do sleep 1
done
kill $PING
Or something like that.

Last edited by berndbausch; 06-08-2020 at 09:28 AM.
 
1 members found this post helpful.
Old 06-08-2020, 10:49 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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?

Last edited by crts; 06-08-2020 at 10:55 PM.
 
Old 06-09-2020, 12:07 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by crts View Post
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.
 
Old 06-09-2020, 12:33 AM   #6
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
@berndbausch

There is a new question based on top of this question.

https://www.linuxquestions.org/quest....php?p=6132308

Can you please help me with that?
 
Old 06-09-2020, 01:56 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by berndbausch View Post
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.
 
1 members found this post helpful.
Old 06-09-2020, 01:58 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by blueray View Post
@berndbausch

There is a new question based on top of this question.

https://www.linuxquestions.org/quest....php?p=6132308

Can you please help me with that?
you ought set this thread solved if it was solved and also would be nice to say thanks (if you think so) - by clicking on yes.
 
Old 06-09-2020, 02:11 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by berndbausch View Post
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.

Last edited by crts; 06-09-2020 at 02:14 AM.
 
2 members found this post helpful.
Old 06-09-2020, 02:23 AM   #10
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by crts View Post
it is not obvious that grep would behave this way.
Not obvious, but useful. Thanks for teaching me something new.
 
Old 06-09-2020, 02:25 AM   #11
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
@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.

Last edited by blueray; 06-09-2020 at 03:51 AM.
 
  


Reply

Tags
bash, command line



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] No package 'x11' found No package 'xext' found No package 'xdamage' found No package 'xfixes' found No package 'x11-xcb' found Jigsaw Linux From Scratch 14 02-23-2021 08:35 PM
tar - write to stdout & create log files ( from stdout and stderr ) [solved] paziulek Linux - General 2 02-23-2014 12:26 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration