I have a go script for monitoring the rtsp protocol and I don’t understand how to make an expression search in the output after running the script. Made an approximate bash script -

#!/usr/bin/env bash if [ "$(sh run.sh | egrep '(RTSP/1.0) [0-9][0-9][0-9]' | tail -1)" ]; then echo OK #elif egrep 'qwert'; then # echo bad else echo ERROR fi 

I need if I found, say rtsp / 1.0 200, then write OK, and if we allow rtsp / 1.0 400, then BAD, etc. But run the script only once.

  • Save the output sh run.sh to a temporary file and run grep on this file - Alexey Ten

2 answers 2

I can offer this preparation:

 #!/bin/bash ./run.sh | while IFS= read -r line do if [[ $line =~ "regex_pattern" ]]; then echo "есть совпадение" fi done 

The output of the script is sent to an infinite while loop, where it is run line by line for a match with regex_pattern .

  • Almost, but not at all, it is necessary that he only deduced one word) - Anuar Mukatov
  • one
    @AnuarMukatov "Almost, but not at all"? )) Then add all the conditions to the description of your question. For example, they brought out the word once, and then what? Script completion? Or repeat the output in a day? - de_frag
  • I beg your pardon, I did not think. Yes, you need to have one word in echo, and not every line of echo and the completion of the script. - Anuar Mukatov
  • @AnuarMukatov can simply add the exit 0 line below echo . - de_frag

Imho is so simpler and smaller frills:

 while read -r line; do [[ "$line" =~ RTSP/1.0 200 ]] && { echo DONE exit 0 # Это чтобы потом можно было обработать результат выполения скрипта если необходимо } [[ "$line" =~ RTSP/1.0 400 ]] && { echo ERROR exit 1 # Это для того-же что и exit 0 } done < <(./run.sh)