Reputation: 3860
Example with the command-line tool flock
.
The documentation just defines the syntax:
flock [options] <file> -c <command>
But -c
seems not to be mandatory.
This line executes a program if locking of a file is possible (the '-n' switch disable wait for lockfile release):
flock -n lockfile.lck echo "File is Locked"
I would like, instead, to make a script like:
flock -n $TMP/lockfile.lck (
echo "Congratulations:"
echo "you have acquired the lock of the file"
)
As you can see, it is nearly the same, but executing more than one line.
Is it possible?
And, for the case when locking is not possible:
flock -n lockfile.lck echo "Lock acquired" || echo "Could not acquire lock"
I would like to know if it can be implemented something like :
flock -n $TMP/lockfile.lck (
echo "Congratulations:"
echo "you have acquired the lock of the file"
) || (
echo "How sad:"
echo "the file was locked."
)
Thanks.
Upvotes: 1
Views: 93
Reputation: 62389
Since the parentheses there are part of the shell syntax, and not commands in their own right, you would have to do something like this:
flock -n lockfile.lock bash -c '( command list )'
Depending on what your exact set of commands are, the necessary quoting/escaping might be a slight additional challenge, but if that gets too hard, then just put your commands in a proper shell script, and call that instead of the bash -c ...
syntax.
EDIT : In fact, using a shell this way, the parentheses are no longer necessary for command grouping (and in fact would be less efficient due to spawning an additional subshell), so this would suffice:
flock -n lockfile.lock bash -c 'command list'
Upvotes: 1