I am running the following inside the makefile:-
@START=$(shell date +%s) && \
echo $${START} &&\
sleep 10s && \
END=$(shell date +%s) && \
echo "$${END} $${START}" &&\
DIFF_SUB=$$(($$END - $$STA开发者_高级运维RT)) && \
echo IT TOOK $${DIFF_SUB} SECONDS
and it outputs to the following:-
1309950228
1309950228 1309950228 IT TOOK 0 SECONDS
The reason you are getting the same result for each $(shell date +%s)
is that each is being executed and substituted at the same time - that is when make
is running the command.
After make
does its substitutions, this is the command the shell sees:
@START=1309950228 && \
echo ${START} &&\
sleep 10s && \
END=1309950228 && \
echo "${END} ${START}" &&\
DIFF_SUB=$(($END - $START)) && \
echo IT TOOK ${DIFF_SUB} SECONDS
If you want the date
command to be run twice with a 10 second time difference between them, you will need the shell to perform the command substitution, not make
:
@START=$$(date +%s) && \
echo $${START} &&\
sleep 10s && \
END=$$(date +%s) && \
echo "$${END} $${START}" &&\
DIFF_SUB=$$(($$END - $$START)) && \
echo IT TOOK $${DIFF_SUB} SECONDS
Note the double $$
and the removal of the shell
make
command.
make
expands all variable and function references in the body of a rule before executing any of it. Since you're using $(shell)
to run the date
command, that actually means that both calls to date
are getting run before sleep
.
Keep in mind that the body of a rule is already executed using the shell, so generally it's redundant to use $(shell)
in that context anyway -- just let the shell do it's thing, instead of trying to get make
to do part of it. See camh's answer for an example of how to do this.
You can read more about the relationship between make
and the shell here: Shell commands in GNU make.
精彩评论