I have a command that outputs a stream say adb logcat
. I want to pipe that to grep until I find the first match of a certain query. At this point I want to stop the first command:
I've tried:
MY_VARIABLE=$(adb logcat | grep -m 1 'my variable (.*)')
But the problem is that the process will not stop even if grep finds the first match.
My guess is that I need to run adb logcat in nohup, pipe that command to grep, then stop it when done. But I'm not sure how to do this.
You need to unbuffer the input to
grep
, that is unbufferadb logcat
. A similar example shows another command (a Perl one-liner that prints numbers 1-13, one after another, pausing 1 second in between numbers). Unbuffering the Perl command with$| = 1;
does the trick and causesgrep
to return after 3 seconds instead of after the Perl command exits, which is after 13 seconds: