Pipe Terminal Output To Notification, If Output Contains Specific Phrase

monitoringnotificationsterminal

I have a long series of commands that I run on a semi-regular basis. Sometimes, completion of the commands requires user input via password prompt or other responses and sometimes information is output in the course of these commands that I need to copy to a log elsewhere. Mostly, though, the script can run unattended as it processes its lengthy series of tasks.

I've come across dozens of scripts and tools that will tell you when your command has finished running. But that's not what I need. And I've come across tools that should be able to convert varied output into a notification, but there's no accompanying documentation explaining how to do this.

So, if I wanted to be sent a banner notification through Notification Center every time the phrase "space cats" was output during the running of my command, how would I trigger that notification to be sent?

Best Answer

If I understand you correctly, you can access the output of your scripts. Then you can do something like this:

#!/bin/bash

# Replace the echo with your script
echo "space cats" | grep "space cats"

if [[ "$?" -eq "0" ]]; then
    osascript -e 'display notification "Attention!" with title "Notification" subtitle "I found space cats" sound name "Submarine"'
fi