Yesterday I got some clarification around using flock and some simple concepts are becoming clear to me.
My question now is around sub-shells. Take a look at this block
( flock -s 200 # ... commands executed under lock ... ) 200>/var/lock/mylockfile
My understanding is 200>/var/lock/mylockfile runs before flock -s 200, yet if I try something like this
( echo This is a sub-shell ) command
I get an error from BASH
-bash: syntax error near unexpected token `command'
Introducing a semi-colon
( echo This is a sub-shell ); command
fixes the error, but causes command to run after the sub-shell returns.
So I'm wondering how the initial example causes the redirect to run first. Is it something to do with precedence of the > over sub-shell (list) notation? Looking in the BASH manpage I find
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.
200>/var/lock/mylockfile is, in this context, information about how the subshell should be configured (how its redirection is supposed to be set up). It's not a separate command.
This is true in the same way that
echo "foo" >bar.txt
...always does the redirection first, as does
>bar.txt echo "foo"
In neither of these cases does order matter at all.
A subshell is similar in this respect -- it's just a compound command, and like any other command, it can have redirections specified either at its beginning or its end.
200>/var/lock/mylock; only creates mylock but that's all. For instance,
$ 200>/var/lock/mylock; $ echo "hello" >&200 bash: 200: bad file descriptor.
doesn't work. If you want this to run, add exec :
$ exec 200>/var/lock/mylock; $ echo "hello" >&200 $ cat /var/lock/mylock hello
Now about subshells : when writing
( ... ) 200>/var/lock/myfile
bash creates a child using fork(), then the child redirects 1 to 200 using dup2, then parses and executes the code inside ( ... ).
Thus if you want to create the file before the subshell, a solution is
exec 200> /var/lock/file ( ... ) >&200