Bash: loop through Procces output and terminate the process

I need help with the following:

I use linux for script commands sent to the device. I need to send the grep logcat command to the device and then iterate my output as it is generated and find a specific line. Once this line is found, I want my script to go to the next command.

in pseudo code

for line in "adb shell logcat | grep TestProccess" do if "TestProccess test service stopped" in line: print line print "TestService finished \n" break else: print line done 
+4
source share
3 answers
 adb shell logcat | grep TestProcess | while read line do echo "$line" if [ "$line" = "TestProces test service stopped" ] then echo "TestService finished" break fi done 
+5
source
 adb shell logcat | grep -Fqm 1 "TestProcess test service stopped" && echo "Test Service finished" 

grep flags:

  • -F - treat string literally, not as a regular expression
  • -q - do not print anything to standard output
  • -m 1 - stop after the first match

The command after && is only executed if grep finds a match. As long as you “know” grep will eventually match up and want to continue unconditionally, as soon as he returns, just leave && ...

+1
source

You can use the while loop.

 adb shell logcat | grep TestProccess | until read line && [[ "$line" =~ "TestProccess test service stopped" ]]; do echo $line; done && echo -n "$line\nTestService finished" 
0
source

All Articles