Search code examples
linuxbashunixfile-descriptorflock

How to use variable for file descriptor with flock?


I would like to do like this

( flock --nonblock ${descr} || exit

... 

) ${descr}>/tmp/smth.lock

but I got errors:

syntax error near unexpected token `${descr}'
`) ${descr}>/tmp/smth.lock'

So is it possible to use variable for descriptor? Could anybody help? Thank you!


Solution

  • The syntax is {descr}>/tmp/smth.lock (no dollar sign) to allocate the file descriptor and assign it to the variable descr. After this, you could use descr as a normal variable.