Skip to main content
2 of 8
added 315 characters in body
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11

From OP's comments it sounds like all mycommand output is going to stderr ... maybe. OP has also clarified that mycommand is writing directly to the log file.

I'm going to assume we don't know, for sure, if output is going to stdout, stderr or a mix.

For demo purposes I'll use the following:

% cat mycommand
#!/bin/zsh

for str in "starting ..." "running ..." "ERROR: had a problem" "stopping"
do
    if [[ "${str}" =~ ERROR: ]]
    then
        echo "${str}" >&2               # remove ">&2" to write to stdout
    else
        echo "${str}"                   # append ">&2" to write to stderr
    fi
    sleep 2
done

One approach benefiting from zsh's ability to use multiple redirections:

% ./mycommand 1> >(tee /dev/tty | grep --line-buffered '^ERROR:' | espeak) 2>&1
starting ...
running ...
ERROR: had a problem               # espeak output heard on speakers
stopping

NOTES:

  • regardless of modifying mycommand to send output to stdout, stderr or a mix of stdout/stderr ...
  • in my system I hear ERROR <pause> had a problem over my speakers as the line ERROR: had a problem was printed to the terminal
  • --line-buffered is a GNU grep extension that insures we hear the espeak output at the same time as we see the ERROR: line
  • OP can install espeak via sudo apt install espeak
  • OP can replace espeak with whatever sound producing program they wish
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11