I'm trying to get a lock on a file with bash. I've found some docs, including this nice answer:
Linux flock, how to "just" lock a file?
It works, BUT I cannot release the file descriptor using a variable.
This WORKS
LOCK_FILE="lock.lock"
# Acquire lock - lock_fd is always 10 in my machine
exec {lock_fd}>$LOCK_FILE || exit 1
flock "$lock_fd" || exit 1
# Release lock - OK
exec 10>&-
See the last line to release the lock: exec 10>&-
. If I put the number it works
BUT, this DOES NOT work:
LOCK_FILE="lock.lock"
# Acquire lock
exec {lock_fd}>$LOCK_FILE || exit 1
flock "$lock_fd" || exit 1
# Release lock - not found
exec $lock_fd>&-
I use the variable exec $lock_fd>&-
, as shown in the answer above (and in some other documentation I've found around)
./lock.sh: line 6: exec: 10: not found
Somebody could tell what is wrong? Thank you.