如何尾随adb logcat的输出并在每行中执行命令

问题描述 投票:-1回答:1

[我正在尝试执行this post,但我不想从文件中读取,而是要“订阅” adb logcat的输出,并且每次记录新行时,我都会在该行上运行一些代码。

我尝试了这些代码,但是没有用

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

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

简单的方法是什么?在此先感谢

linux bash adb logcat
1个回答
2
投票

以下两个解决方案应该起作用。我通常更喜欢第二种形式,因为wile循环在当前进程中运行,因此我可以使用局部变量。第一种形式在子进程中运行while循环。

在子进程中循环时:

#!/bin/bash

adb logcat |
while read -r line; do
  echo "${line}"
  processLine "${line}"
done

当前进程中的循环:

#!/bin/bash

while read -r line; do
  echo "${line}"
  processLine "${line}"
done < <(adb logcat)
© www.soinside.com 2019 - 2024. All rights reserved.