Home > Blockchain >  tail -f | grep in if statement
tail -f | grep in if statement

Time:10-09

I'm running from bash script something like

$(command -options)&
export SC_PID=$!    
if tail -f <log_filename.txt> | grep --line-buffered -E "(some expression)"; then
        kill -STOP $SC_PID
fi

but it's writing "(some expression)" in the command line output instead of killing the process. Note that log_filename.txt is a log file where's being written output from $(command -options) in real time. What am I doing wrong?

CodePudding user response:

Your pipe (tail -f ... | grep ...) will never end.

Add -m 1 to your GNU grep to exit after first match.

CodePudding user response:

your if statement with "tail -f" can't finished until you break it, so it just can't move to next step. try to split text by lanes, like the following:

$(command -options)&
export SC_PID=$!    
tail -f <log_filename.txt)|while read; do
  if (echo "$REPLY"|grep --line-buffered -E "(some expression)"); then
    kill -STOP $SC_PID
  fi
done
  • Related