The traditional tool for comparing time stamps to determine whether work needs to be performed or not is make
. Its default behavior is to calculate a dependency chain for the specified target(s) and determine whether any of the dependent files have changed; if not, the target does not need to be remade. This is a great tool for avoiding recompilation, but it easily extends to other tasks.
In concrete terms, you'd create a flag file (say, .made
) and specify it as dependent on your file
. Now, if file
has changed, .made
needs to be recreated, and so make
will run the commands you specify to do so. In this scenario, we would run a simple piece of shell script, then touch .made
to communicate the latest (successful) run time to future runs.
What remains is for the recipe to run different commands at different times. My approach to that would be a simple case
statement. Notice that make
interprets dollar signs, so we need to double those dollar signs which should be passed through to the shell.
.made: file
case $$(date +%H:%M) in \
09:* | 10:* | 11:[0-2]? ) \
scriptA.sh ;; \
11:[3-5]? | 1[2-6]:* | 17:[0-3]? | 17:4[0-5]) \
scriptB.sh;; \
17:4[6-9] | 17:5? | 1[89]:* | 2?:* | 0[0-8]:* ) \
scriptC.sh;; \
esac
touch $@ # $@ expands to the current target
The entire case
statement needs to be passed as a single logical line to the shell, so we end up with those pesky backslashes to escape the newlines.
Also notice that make
is picky about indentation; each (logical) line in the recipe should be preceded by a literal tab character.
The default behavior of make
is to run the first target in the file; this Makefile
only contains one target, so make
is equivalent to make .made
.
Also notice that make
cares about exit codes; if scriptA
, scriptB
, or scriptC
could exit with a non-zero exit status, that is regarded by make
as a fatal error, and the .made
file will not be updated. (You can easily guard against this, though.)