Search code examples
bashpipestdoutstdintee

Monitor changes in stdin from a bash script


I would like to make a bash tool as a script to get notified when a change in the output of a given command happens. Use cases would be for instance to get a notification when the output of a long script changes. I would like it to be used on the same model as the tee command :

any_long_script_to_run | my_tool 

The output would be then be transparently copied to stdout, but I would like to define as well a custom function (play a sound, display a notification...) to run each time a new line is written for instance.

Is there any clever way of doing this in bash ?

Thanks a lot !


Solution

  • The command to create a notification depends on what OS/window manager you're running, but one simple answer is:

    any_long_running_command | while IFS= read -r line; do
        printf "\a%s\n" "$line"
    done
    

    \a is the "bell" character, and usually makes a sound.