9mjb
9mjb

Reputation: 583

Howto use makefile target with date?

I want something like this (date (from shell) in the target filename)

bakfile=mybackup.$(date +%Y%m%d).tar
$(bakfile):
     tar -cf $@ /source-dir

Or maybe with the date in the target spec?

mybackup.$(date +%Y%m%d).tar:
     tar -cf $@ /source-dir

Upvotes: 0

Views: 402

Answers (2)

MadScientist
MadScientist

Reputation: 100836

A makefile is not a shell script. A makefile can contain shell scripts, but you can't just write shell operations directly anywhere in a makefile and expect it to work like the shell.

I recommend:

date := $(shell date +%Y%m%d)

then use the variable $(date) wherever you want the date string. Running it multiple times is a race condition risk, if you run it right around midnight the value might change between two different invocations.

Upvotes: 2

Beta
Beta

Reputation: 99094

You have to make a slightly greater effort, if you want to invoke shell commands from within a makefile:

bakfile=mybackup.$(shell date +%Y%m%d).tar

You can do it in the target name too, but I advise against that-- it's hard to read.

Upvotes: 0

Related Questions