Search code examples
linuxbashadblogcat

How to tail output from adb logcat and execute a command each new line


I'm trying to do very likely to this post but instead of reading from a file I want to "subscribe" to the output of adb logcat and every time a new line is logged I will run some code on this line.

I tried these codes, but none worked

tail -f $(adb logcat) | while read; do 
    echo $read;
    processLine $read;
done

or

adb logcat >> logcat.txt &
tail -f logcat.txt | while read; do 
    echo $read;
    processLine $read;
done

What is the simples way to do this? Thanks in advance


Solution

  • The following two solutions should work. I generally prefer the second form as the wile loop is run in the current process and so I can use local variables. The first form runs the while loop in a child process.

    While loop in child process:

    #!/bin/bash
    
    adb logcat |
    while read -r line; do
      echo "${line}"
      processLine "${line}"
    done
    

    While loop in current process:

    #!/bin/bash
    
    while read -r line; do
      echo "${line}"
      processLine "${line}"
    done < <(adb logcat)