首页 > 解决方案 > 退出 adb logcat 的 Bash 脚本

问题描述

我正在尝试编写一个脚本来启动应用程序并检查 logcat 中的字符串。一旦我找到字符串。我想退出 logcat 并杀死应用程序

if adb shell monkey -p com.app.dev -c android.intent.category.LAUNCHER 1 | adb logcat | grep --line-buffered 'match_text' 
then 
   adb shell am force-stop com.app.dev
fi

我的脚本看起来像上面那样。但是在我在 logcat 中找到匹配的行之后,由于 logcat 已经接管了 bash 命令进程,因此其中的代码永远不会执行。

标签: androidbashadblogcat

解决方案


你应该做这样的事情

mkfifo temp
# start you app here, I would use am
adb shell monkey -p com.app.dev -c android.intent.category.LAUNCHER 1
adb logcat > temp &
pid=$!
if grep -qm1 --line-buffered 'match_text' < temp
then
    kill $pid
    rm temp
    adb shell am force-stop com.app.dev
fi

推荐阅读