Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a way to programatically watch the output of a command until a particular string is observed and then exit. This is very similar to this question, but instead of tailing a file, I want to 'tail' a command.

Something like:

watch -n1 my_cmd | grep -m 1 "String Im Looking For"

(But this doesn't work for me.)

UPDATE: I need to clarify that 'my_cmd' does not continuously output text but needs to be repeatedly called until the string is found (which is why I thought of the 'watch' command). In this respect, 'my_cmd' is like many other unix commands such as: ps, ls, lsof, last, etc.

share|improve this question
    
I would have thought it was possible to tail -f a program output just as well as a file... Am I wrong? –  M. Joanis Jan 6 '12 at 0:52
    
@Joanis. You're right, but in my case 'my_cmd' doesn't continuously produce output and must be repeatedly called (much like most commands: ps, ls, lsof, etc) –  gdw2 Jan 6 '12 at 4:01

4 Answers 4

up vote 4 down vote accepted

Use a loop:

until my_cmd | grep -m 1 "String Im Looking For"; do : ; done

Instead of :, you can use sleep 1 (or 0.2) to ease the CPU.

share|improve this answer
    
Works perfectly. Thanks. –  gdw2 Jan 6 '12 at 14:11
my_cmd | tail +1f | sed '/String Im Looking For/q'

If tail doesn't support the +1f syntax, try tail -f -n +1. (The -n +1 tells it to start at the beginning; tail -f by default starts with the last 10 lines of output.)

share|improve this answer
    
Please see my update to the question. –  gdw2 Jan 6 '12 at 4:00

Append the result of your program calls to a file. Then tail -f that file. That way it should work... I hope.

When you restart calling that program you'll have to erase the file or append some gibberish to it just so it doesn't match again right away what you were looking for.

share|improve this answer
watch -e "! my_cmd | grep -m 1 \"String Im Looking For\""
  • ! negates the exit code of the command pipeline
  • grep -m 1 exits when string is found
  • watch -e returns if any error has occured

But this can be improved to actually display that matched line, which is thrown away so far.

share|improve this answer
    
Thanks for the detailed explanation, but it doesn't work for me. My watch command (CentOS) doesn't have the -e flag (which shouldn't really matter). More importantly, though, when the string is found, watch continues to run and does not exit. It seems that when grep -m exits, it only exits kills my_cmd, but not watch. –  gdw2 Jan 6 '12 at 14:05
    
Nope, is does matter!, the "-e" flag is ment for leaving watch when the command has an error code different from 0. Since its not present watch is about to continue on your platform. Anyhow, good to know, on my Ubuntu 11.10 installation everything is fine. I have also sometimes troubles with Mac OSX regarding very very outdated commandline tools and I am using mac ports so far to get more current software. –  math Jan 9 '12 at 10:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.