-1

I have these 2 scripts, one is writing to the file:

#!/usr/bin/env bash

while true; do
   sleep 1;
   echo "$(uuidgen)"  >> /tmp/cprev.stdout.log
done;

the other is reading the last 10 lines and overwriting the file with those 10 lines:

#!/usr/bin/env bash

while true; do
   sleep 5;
   inotifywait -e modify /tmp/cprev.stdout.log | tail /tmp/cprev.stdout.log > /tmp/cprev.stdout.log
done;

for some reason the tail command is truncating the file - what I want to do is write to the file only when the tail command finishes getting all 10 lines from the file, how can I do that?

what actually happens:

  1. tail truncates file
  2. tail reads 0 lines

but what I want to do:

  1. tail reads 10 lines
  2. tail truncates files
  3. tail writes 10 lines from above

how can I do that?

  • 2
    Hint: tail isn't writing to the file, the shell is. And the shell truncates it before tail gets a chance to read anything – Fox Mar 26 '20 at 01:03

1 Answers1

0

I guess this works:

#!/usr/bin/env bash

while true; do
   sleep 5;
   inotifywait -e modify /tmp/cprev.stdout.log | while read line; do
      lines="$(tail /tmp/cprev.stdout.log)"
      echo "$lines" > /tmp/cprev.stdout.log
   done;
done;

but I am looking for something sleeker if possible