简体   繁体   English

如果在脚本输出中找到新行,则执行命令

[英]Execute a command if new line is found in script output

I am having huge difficulties writing a simple bash script. 我在编写一个简单的bash脚本时遇到了很大的困难。

I basically want to monitor a debug log 24/7 and listen for a new line. 我基本上想监视24/7调试日志并侦听新行。 If there is a new line I want it to execute a command. 如果有新行,我希望它执行命令。

logcat | grep com.amazon.firelauncher/.Launcher

When I run this code, I get a live debug window, I want to execute a command (reboot for an example) whenever a new line pops up in that command. 当我运行此代码时,我得到一个实时调试窗口,我想在每条命令中弹出新行时执行一条命令(例如重新启动)。

How can this be done? 如何才能做到这一点? I tried sending the output to a file and monitoring that file for filesize change, but it does not work. 我尝试将输出发送到文件并监视该文件的文件大小更改,但是它不起作用。 I really need assistance! 我真的需要协助!

You should be able to use read for this: 您应该能够为此使用read

#!/bin/bash
while logcat | grep "com.amazon.firelauncher/.Launcher" | read -r line; do
    echo "Text read from logcat"
    /path/to/executable
done

This was my final answer, but I would not have concluded it if it was not for the help of Martin Konecny, he did all the heavy lifting. 这是我的最终答案,但是如果没有Martin Konecny的帮助,我将不会下结论,他会尽一切努力。

#!/bin/bash
am monitor | while read -r line; do
    if [[ $line == *"firelauncher"* ]]
    then
      am start com.newlauncher.launcher
    fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM