Warm tip: This article is reproduced from stackoverflow.com, please click
bash shell unix

How to wait till a particular line appears in a file

发布于 2020-03-27 15:40:06

Is it possible to write a script that does not proceed till a given line appears in a particular file?

For example I want to do something like this:

CANARY_LINE='Server started'
FILE='/var/logs/deployment.log'

echo 'Waiting for server to start'
.... watch $FILE for $CANARY_LINE ...
echo 'Server started'

Basically, a shell script that watches a file for line (or regex).

Questioner
pathikrit
Viewed
104
Xavier S. 2014-09-23 02:03

We have to be careful with loops.

For example if you want to check for a file to start an algorithm you've probably have to do something like that:

  FILE_TO_CHECK="/var/logs/deployment.log"
  LINE_TO_CONTAIN="Server started"
  SLEEP_TIME=10
  while [ $(cat FILE_TO_CHECK | grep "${LINE_TO_CONTAIN}") ]
  do
      sleep ${SLEEP_TIME}
  done

  # Start your algorithm here

But, in order to prevent an infinite loop you should add some bound:

  FILE_TO_CHECK="/var/logs/deployment.log"
  LINE_TO_CONTAIN="Server started"
  SLEEP_TIME=10
  COUNT=0
  MAX=10
  while [ $(cat FILE_TO_CHECK | grep "${LINE_TO_CONTAIN}") -a ${COUNT} -lt ${MAX} ]
  do
      sleep ${SLEEP_TIME}
      COUNT=$(($COUNT + 1))
  done

  if [ ! $(cat FILE_TO_CHECK | grep "${LINE_TO_CONTAIN}") ]
  then
    echo "Let's go, the file is containing what we want"
    # Start your algorithm here
  else
    echo "Timed out"
    exit 10
  fi