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

Oozie— java: command not found - shell action
How to execute system command with an argument that contains spaces
Why the shell doesn't write the good hour and give me the year?
shell script with dynamic variable
Trying to get files from today date and result is displaying yesterday date files as well (Unix Korn Shell)
Nagios plugin that runs at 2pm daily
remove with double-asterisk wildcards does not work [duplicate]
Zero byte output File in Unix Environment
Format data using Awk Statemnt
Passing environment variables over ssh for remote expansion
Providing input for additional authentication using Plink
Way to create multiline comments in Bash?
How do you execute a SHELL script in multiple directories without having to generate a run script
How to stop GNU make from expanding dollar signs in MAKEFILE_LIST?
POSIX shell: escaping line-continuations in backquote command-substitutions
ms-dos command shell sort returns “Input file specified two times.”

Categories

HOME
sonarqube
assembly
telegram
github-for-windows
phpmyadmin
object
numbers
sh
package
solver
knitr
mql5
drop-down-menu
dkim
line
zerobrane
azure-iot-hub
entitymanager
package.json
dynamic-sql
mapstruct
oledb
googlebot
spring-async
ember-cli
tosca
short-url
windows-server-2003
jquery-multiselect
angularjs-components
genexus-sd
jaws
vex
calculation
recurring-events
anki
scatter3d
postback
sql-server-2000
angularjs-material
biztalk-rule-engine
gnat-gps
classcastexception
openwhisk
video-capture
swagger-php
metaprogramming
p4merge
ogc
doc
android-ibeacon
ssrs-2014
facebook-social-plugins
openresty
context-sensitive-grammar
backbone-collections
excel-dna
structuremap4
hadoop-2.7.2
dotnetnuke-7
watch-os-2
ng-grid
xdoclet
lean
clipboard.js
riot
kiosk
ikiwiki
uiactionsheet
invoke-sqlcmd
themoviedb-api
dllimport
pypiserver
ess
rpart
wso2greg
nsurlsessiondatatask
variadic-templates
rapidsvn
theos
cosine-similarity
gii
live555
matrix-inverse
wxformbuilder
jquery-slider
memset
mutual-authentication
clickbank
xmltodict
windows-embedded-standard
gtm-oauth2
ebay-lms
inappsettingskit
route-provider
abstract-data-type
mysql-error-2013
http-daemon
codebase
ruby-1.8
pos-for-.net
duplex
sun
google-wave
localizable.strings
unmanagedresources
commercial-application

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