|9||This (ba)sh script defines some useful functions for dependency checking and file locking.|
Dependency checking is implemented in function "needs_update", which is called with two or more parameters (all paths to files or directories). The first parameter is the target file, which depends on the remaining files listed as parameters. If one of the prerequisite files is more recent than the target files, then the function returns "true", i.e., the target file needs to be updated. Otherwise, the function returns "false", i.e., the target file is up to date w.r.t. the given dependencies.
if needs_update target source1 source 2; then
cat source1 source2 > target
The file locking (which tries hard to be NFS-safe, although it isn't completely) is implemented in two functions:
"lockfile_create" creates a lock for a given path in the file system, which is typically a target file that is being generated. If the function returns "true", locking was successful and processing should proceed. If the function returns false, locking failed (e.g., because another process has already locked the same file), and processing should skip this target file.
If a lock was successfully acquired, it must subsequently be freed (i.e., deleted) by calling "lockfile_delete".
if lockfile_create target; then
echo "Hello!" > target
Finally, there is a combination function, "needs_update_and_lock," which is called exactly like "needs_update", but attempts to acquire a lock on the target file in case it needs to be updated.
if needs_update_and_lock target src1 src2; then
cat src1 src2 > target