2

Never coded on bash but need something urgent. Sorry if this is not the norm but would really like to get some help.

I have some messages that are thrown to stdout, Depending on the message type (the message is a string with the word "found") I need the bash script to beep.

So far I've come up with this.

  output=$(command 1) # getting stdout stream?
  while [ true ]; do
    if [ "$output" = "found" ]; then # if the stdout has the word "found" 
     echo  $(echo -e '\a') # this makes the beep sound
    fi
  done

I'm not sure where/how to add grep or awk command to check for the string that has the word "found" and only return "found" so that in the if condition it can check against that word.

Thanks!

sylvian
  • 709
  • 2
  • 9
  • 19
  • Regarding the bell, slightly shorter and more portable is `printf '\a'` in place of `echo $(echo -e '\a')`. A simple `echo -e '\a'` will also work; there's no need to capture the first echo and re-echo it. – chepner Nov 01 '13 at 12:59

1 Answers1

11

You can do something as simple as:

command | grep -q 'found' && echo -e '\a'

If the output of command contains the text "found", then grep will return with a zero exit status, so the echo command will be executed, causing the beep.

If the output does not contain "found", grep will exit with status 1, and will not result in the echo.

Depending on what you need to make the beep work, just replace anything after the &&. The general syntax would be something like:

command | grep -q "$SEARCH" && command_if_found || command_if_not_found
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • 5
    Recommend `grep -q` or `grep >/dev/null`. – John Kugelman Oct 31 '13 at 18:50
  • Thanks, this makes so much more sense. So I added to the script but it looks like that command is not getting any messages from the stdout. Do you know what's the proper way to get stdout messages? This is what I have #/bin/bash SEARCH="found" while [ true ]; do $(command | grep -q "$SEARCH" && echo -e '\a') done – sylvian Oct 31 '13 at 19:21
  • You don't need a loop. Try breaking it down into components. Look at the output of `command`, then at `command | grep 'found'`, and so on. Maybe try looking at things like `command > /dev/null` too - perhaps `command` is outputting to stderr? – cmbuckley Oct 31 '13 at 19:22
  • sorry I'm a bit confused. The terminal constantly outputs messages so I need it to beep every time the string contains the word "found". I'm not really sure what "command" does. Is that a variable for the stdout messages that I can use in the bash script? I don't know how it's possible to do without a loop since I need it to constantly check the messages. – sylvian Oct 31 '13 at 20:01
  • I was using `command` from your original example :-) If the command is running continuously, and you want to beep occasionally if it outputs a certain string, then you should use `grep`'s `--line-buffered` option. (See http://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream) – cmbuckley Oct 31 '13 at 21:21
  • You may even be able to do `command | awk '/found/{printf "\a"}'`. – cmbuckley Oct 31 '13 at 21:46