Personal tools
  • Help

Image:Utilities.sh

From NITRC Wiki

Jump to: navigation, search

Utilities.sh (file size: 4 KB, MIME type: application/x-sh)

Warning: This file may contain malicious code. You may compromise your system by opening or running it.

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.

Example:

if needs_update target source1 source 2; then cat source1 source2 > target fi

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".

Example:

if lockfile_create target; then echo "Hello!" > target lockfile_delete target fi

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.

Example:

if needs_update_and_lock target src1 src2; then cat src1 src2 > target lockfile_delete target fi

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeUserDimensionsFile sizeComment
current18:39, 8 April 2010Moderator (Talk | contribs)4 KBThis (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. Example: if needs_update target source1 source 2; then cat source1 source2 > target fi 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". Example: if lockfile_create target; then echo "Hello!" > target lockfile_delete target fi 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. Example: if needs_update_and_lock target src1 src2; then cat src1 src2 > target lockfile_delete target fi

There are no pages that link to this file.

Powered by MediaWiki
  • This page was last modified 18:39, 8 April 2010.
  • This page has been accessed 272 times.
  •