简体   繁体   中英

Execute a command if new line is found in script output

I am having huge difficulties writing a simple bash script.

I basically want to monitor a debug log 24/7 and listen for a new line. 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:

#!/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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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