Skip to main content
Rollback to Revision 6
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
% cat mycommand
#!/bin/zsh

for str in "starting ..." "running ..." "ERROR: had a problem" "stopping"
do
    if [[ $str"${str}" ==~ *ERRORERROR:* ]]
    then
        printecho -r"${str}" -u2>&2 -- $str             # remove "-u2"">&2" to write to stdout
    else
        printecho -r"${str}" -- $str                 # Addappend "-u2"">&2" to write to stderr
    fi
    sleep 2
done
  • 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
  • tee /dev/tty - insures all data coming in on stdout is copied to the terminal at the same time asbefore being piped to grep
  • 2>&1 - insures all data coming in on stderr is redirected to stdout (which means stderr is also routed through the tee /dev/tty | grep ... | espeak pipeline)
  • grep is used to extract just the lines we wish to pass to espeak; if looking for multiple strings, you can use grep -e '^string1' -e '^string2' or switch you'll likely need to enable extended regexp which has an alternation operatorregex support (eg, grep -E '^(string1|string2...)').
  • --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

This solution (above) will speak the entire line when said line starts withcontains the string ERROR^ERROR:.

Speaking just the word error or warning for each occurrence ofwhen there's a case insensitive match on the strings ERROR and/or Warning in the line:

./mycommand 1> >(tee /dev/tty | grep -oi -E --line-buffered 'ERROR|WARNING''(ERROR|WARNING)' | espeak) 2>&1
% cat mycommand
#!/bin/zsh

for str in "starting ..." "running ..." "ERROR: had a problem" "stopping"
do
    if [[ $str = *ERROR:* ]]
    then
        print -r -u2 -- $str            # remove "-u2" to write to stdout
    else
        print -r -- $str                # Add "-u2" to write to stderr
    fi
    sleep 2
done
  • 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
  • tee /dev/tty - insures all data coming in on stdout is copied to the terminal at the same time as being piped to grep
  • 2>&1 - insures all data coming in on stderr is redirected to stdout (which means stderr is also routed through the tee /dev/tty | grep ... | espeak pipeline)
  • grep is used to extract just the lines we wish to pass to espeak; if looking for multiple strings, you can use grep -e '^string1' -e '^string2' or switch to extended regexp which has an alternation operator (grep -E '^(string1|string2...)').
  • --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

This solution (above) will speak the entire line when said line starts with the string ERROR:.

Speaking just the word error or warning for each occurrence of a case insensitive match on the strings ERROR and/or Warning in the line:

./mycommand 1> >(tee /dev/tty | grep -oi -E --line-buffered 'ERROR|WARNING' | espeak) 2>&1
% 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
  • 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
  • tee /dev/tty - insures all data coming in on stdout is copied to the terminal before being piped to grep
  • 2>&1 - insures all data coming in on stderr is redirected to stdout (which means stderr is also routed through the tee /dev/tty | grep ... | espeak pipeline)
  • grep is used to extract just the lines we wish to pass to espeak; if looking for multiple strings you'll likely need to enable extended regex support (eg, grep -E ...)
  • --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

This solution (above) will speak the entire line when said line contains the string ^ERROR:.

Speaking just the word error or warning when there's a case insensitive match on the strings ERROR and/or Warning in the line:

./mycommand 1> >(tee /dev/tty | grep -oi -E --line-buffered '(ERROR|WARNING)' | espeak) 2>&1
In zsh (or bash) generally prefer = over =~ as the latter use system regexps which on many systems don't work properly if input is not encoded as per user's locale
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k
% cat mycommand
#!/bin/zsh

for str in "starting ..." "running ..." "ERROR: had a problem" "stopping"
do
    if [[ "${str}"$str =~= ERROR*ERROR:* ]]
    then
        echo "${str}"print >&2-r -u2 -- $str            # remove ">&2""-u2" to write to stdout
    else
        echo "${str}"print -r -- $str                # appendAdd ">&2""-u2" to write to stderr
    fi
    sleep 2
done
  • 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
  • tee /dev/tty - insures all data coming in on stdout is copied to the terminal beforeat the same time as being piped to grep
  • 2>&1 - insures all data coming in on stderr is redirected to stdout (which means stderr is also routed through the tee /dev/tty | grep ... | espeak pipeline)
  • grep is used to extract just the lines we wish to pass to espeak; if looking for multiple strings you'll likely need, you can use grep -e '^string1' -e '^string2' or switch to enable extended regex supportregexp which has an alternation operator (eg, grep -E '^(string1|string2...)').
  • --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

This solution (above) will speak the entire line when said line containsstarts with the string ^ERRORERROR:.

Speaking just the word error or warning when there'sfor each occurrence of a case insensitive match on the strings ERROR and/or Warning in the line:

./mycommand 1> >(tee /dev/tty | grep -oi -E --line-buffered '(ERROR|WARNING)''ERROR|WARNING' | espeak) 2>&1
% 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
  • 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
  • tee /dev/tty - insures all data coming in on stdout is copied to the terminal before being piped to grep
  • 2>&1 - insures all data coming in on stderr is redirected to stdout (which means stderr is also routed through the tee /dev/tty | grep ... | espeak pipeline)
  • grep is used to extract just the lines we wish to pass to espeak; if looking for multiple strings you'll likely need to enable extended regex support (eg, grep -E ...)
  • --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

This solution (above) will speak the entire line when said line contains the string ^ERROR:.

Speaking just the word error or warning when there's a case insensitive match on the strings ERROR and/or Warning in the line:

./mycommand 1> >(tee /dev/tty | grep -oi -E --line-buffered '(ERROR|WARNING)' | espeak) 2>&1
% cat mycommand
#!/bin/zsh

for str in "starting ..." "running ..." "ERROR: had a problem" "stopping"
do
    if [[ $str = *ERROR:* ]]
    then
        print -r -u2 -- $str            # remove "-u2" to write to stdout
    else
        print -r -- $str                # Add "-u2" to write to stderr
    fi
    sleep 2
done
  • 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
  • tee /dev/tty - insures all data coming in on stdout is copied to the terminal at the same time as being piped to grep
  • 2>&1 - insures all data coming in on stderr is redirected to stdout (which means stderr is also routed through the tee /dev/tty | grep ... | espeak pipeline)
  • grep is used to extract just the lines we wish to pass to espeak; if looking for multiple strings, you can use grep -e '^string1' -e '^string2' or switch to extended regexp which has an alternation operator (grep -E '^(string1|string2...)').
  • --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

This solution (above) will speak the entire line when said line starts with the string ERROR:.

Speaking just the word error or warning for each occurrence of a case insensitive match on the strings ERROR and/or Warning in the line:

./mycommand 1> >(tee /dev/tty | grep -oi -E --line-buffered 'ERROR|WARNING' | espeak) 2>&1
deleted 1 character in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11

The following sends Danger Will RonbinsonRobinson to the speaker (max of once per input line) whenever there's a case-insensitive match on the strings ERROR or warning:

The following sends Danger Will Ronbinson to the speaker (max of once per input line) whenever there's a case-insensitive match on the strings ERROR or warning:

The following sends Danger Will Robinson to the speaker (max of once per input line) whenever there's a case-insensitive match on the strings ERROR or warning:

added 482 characters in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
Loading
added 792 characters in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
Loading
added 315 characters in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
Loading
added 315 characters in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
Loading
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
Loading