Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top