So, I am trying to get an exclusive lock on a text file using Flock and a line of text to that file however while doing so it removes whatever text was there earlier.
( flock -n 200 || exit 1
echo "In critical section"
echo text >> file.txt
echo text added to file
) 200>file.txt
echo "After critical section"
So if the file had let's say
123
214
242
232
and after running the script I am expecting something like
123
214
242
232
text
instead, all I get is
text
why would it behave like this, my guess is that it is replacing the original file but I am not sure.