user3649898
user3649898

Reputation: 39

bash how can I wait for next line at the end of file

I don't have any idea how to wait for next line in file. Like I have file from which I give orders to script. actions are: start process, stop process, log active process and exit script.

while true
    do
      while read line
      do
         # do what ever it does 
      done < file
    done

This is what I use to stay in file until exit. How can I hold and wait for next command line when it gets to the last line in file?

Upvotes: 1

Views: 1534

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

Note that your existing code reads the file multiple times, which is not what you're really after.

You could use:

tail -f file |
while read line
do
     # do what ever it does 
done

The tail -f command looks for the end of the file repeatedly. Of course, now you have two problems:

  1. How do you tell tail -f to stop?
  2. How do you get it to read everything that is in the file when it starts?

The answer to Q1 is 'the interrupt key'.

The answer to Q2 is 'if there might be more than 10 lines in the file at start up, then add -n 1000000 or something similar to give you the last million lines of the file'. (Use a larger number if your file might contain more than a million lines.)

Upvotes: 4

Related Questions