Question

I have a Problem to find a good concept on locking files in bash,

Basically I want to achieve the following:

  1. Lock File
  2. Read in the data in the file (multiple times)
  3. Do stuff with the data.
  4. Write new stuff to the file (not necessarily to the end)
  5. Unlock that file

Doing this with flock seems not possible to me, because the file descriptor will just move once to the end of the file.

Also creating a Tempfile fails, because I might overwrite already read lines which is also not possible.

Edit:
Also note that other scripts I do not control might try to write to that file.

So my question is how can I create a lock in step 1 so it will span over steps 2,3,4 till I unlock it again in step 5?

Was it helpful?

Solution

You can do this with the flock utility. You just need to get flock to use a separate read-only file descriptor, i.e. open the file twice. E.g. to sort a file using a intermediate temporary file:

(
    flock -x -w 10 100 || exit 1

    tmp=$(mktemp)

    sort <"$file" >"$tmp"

    cat "$tmp" > "$file"

    rm -f "$tmp"
) 100<"$file"

flock will issue the flock() system call for your file and block if it is already locked. If the timeout is exceeded then the script will just abort with an error code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top