2016-12-24 75 views
1

使用GNU讓4.1,在我的Makefile我可以創建這個::時間戳日誌文件使用`date` Makefile中變量

flush_log: 
     @echo "==> flush logs" 
     cat file > file_`date +%FT%T%Z`.log 

但是誰放:

file_`date +%FT%T%Z`.log 

在Makefile中的var例如在它上面做一個wc

我已經嘗試(沒有成功):

flush_log: 
    @echo "==> flush logs" 
    logfile:=file_$(shell date +%FT%T%Z).log 
    cat my_log > $(logfile) 
    echo `wc -l $(logfile)` 

我得到這個錯誤:

$ make flush_log 
==> flush logs 
logfile:=file_2016-12-24T20:09:52CET.log 
/bin/sh: 1: logfile:=file_2016-12-24T20:09:52CET.log: not found 
Makefile:7: recipe for target 'flush_log' failed 
make: *** [flush_log] Error 127 
$ 

我是從以下簡單https://stackoverflow.com/a/14939434/3313834擴展型變量https://www.gnu.org/software/make/manual/html_node/Flavors.html#Flavors

回答

3

每一行的建議和在使用TAB字符縮進的makefile中(在規則語句後出現)是規則配方的一部分。規則配方中的行被傳遞給shell進行處理,它們不被make解析(除了展開變量/函數引用)。

因此,您的行logfile:=file...正在傳遞給shell並被shell解釋...並且在shell中沒有有效的:=運算符,因此shell認爲整行是單個單詞並嘗試運行那個名字的程序顯然不存在。

你可能想創建一個使變量,必須在配方之外來完成,像這樣:

logfile := file_$(shell date +%FT%T%Z).log 
flush_log: 
     @echo "==> flush logs" 
     cat my_log > $(logfile) 
     echo `wc -l $(logfile)`