shell


Why it is taking the same time?


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 - $$START)) && \
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.

Related Links

My command prompt terminal won't change?
Cygwin can't execute shell script
Reading from kshell output in same file
Can OpenSSL verify software integrity as GPG does?
Why doesn't the shell command find . -name “*.txt” | rm remove all .txt files in the current directory?
to find the size of a file and assign it to a variable in UNIX script
How can I start in command mode (not insert mode) in readline's vi editting-mode
how to control the timing of call to scripts within a shell wrapper
Not able to execute script with parameters within a SSH script
Adding two digital numbers
Replace value of filed IF MATCH
POST with Curl to utilize artifactory API?
Checking if directory in HDFS already exists or not
Update oracle table with start and end times from shell script
Output the test shell script status to webpage
How to run SWI-Prolog from the command line?

Categories

HOME
linkedin
amp-html
pearson-correlation
azure-container-service
odoo-8
openstreetmap
appcelerator-studio
jsonpath
openbugs
libc
3nf
qsqlquery
http-post
job-scheduling
template-engine
cfml
nodemcu
asp.net-core-webapi
sar
breadth-first-search
react-toolbox
parcelable
pimcore
xunit
beautifier
nstimer
firebase-analytics
8051
quantlib
vex
twiml
calculation
custom-controls
spring-security4
clickjacking
getpixel
least-squares
latitude-longitude
parse-tree
xcode7.3
filesystemwatcher
sim-card
numberpicker
resolution
avcapturedevice
jboss-4.2.x
xbmc
openrasta
trojan
static-cast
mouseleave
objectscript
rsa-archer-grc
restore
flex-monkey
centrifuge
spring-cloud-feign
beamer
mediametadataretriever
mobiledoc-kit
dynamics-nav-2016
sciruby
computer-architecture
evolus-pencil
batik
atoi
dronekit-android
mirror
nsurlsessiondatatask
xmltype
openbabel
grouping-sets
jbake
metatrader5
easyrtc
teaspoon
trdion2011
insight.database
create.js
ios8-extension
dot42
paste
authlogic
dsym
ora-06550
kendo-dataviz
isqlquery
traceability
jquery-blockui
xcode3.2
faye
apache-whirr
hibernateexception
google-floodlight
mbeans
umra
cloudfiles
resharper-6.0
burndowncharts
unmanagedresources
httpcookie
movieplayer
mathematical-typesetting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App