4

I need some help with the following:

I use linux to script commands sent to a device. I need to submit a grep logcat command to the device and then iterate its output as it is being generated and look for a particular string. Once this string is found I want my script to move to the following command.

in pseudocode

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
Nakilon
  • 34,866
  • 14
  • 107
  • 142
Nikl
  • 83
  • 2
  • 8

3 Answers3

5
adb shell logcat | grep TestProcess | while read line
do
  echo "$line"
  if [ "$line" = "TestProces test service stopped" ]
  then echo "TestService finished"
       break
  fi
done
Barmar
  • 741,623
  • 53
  • 500
  • 612
1
adb shell logcat | grep -Fqm 1 "TestProcess test service stopped" && echo "Test Service finished"

The grep flags:

  1. -F - treat the string literally, not as a regular expression
  2. -q - don't print anything to standard output
  3. -m 1 - stop after the first match

The command after && only executes if grep finds a match. As long as you "know" grep will eventually match and want to unconditionally continue once it returns, just leave off the && ...

chepner
  • 497,756
  • 71
  • 530
  • 681
0

You could use an until loop.

adb shell logcat | grep TestProccess | until read line && [[ "$line" =~ "TestProccess test service stopped" ]]; do
 echo $line;
done && echo -n "$line\nTestService finished" 
jabbie
  • 2,606
  • 1
  • 17
  • 9