將檔案複製到帶有日期時間的檔案名稱在 bash 中有效,但在 makefile 中無效

將檔案複製到帶有日期時間的檔案名稱在 bash 中有效,但在 makefile 中無效

以下工作在 bash shell 中進行

cp abc.tex "abc-$(date +"%Y-%m-%-d-%H-%M-%S").tex"

但不在 makefile 中。我如何解決它?

這是生成文件:

b:
    cp abc.tex "abc-$(date +"%Y-%m-%-d-%H-%M-%S").tex"

當我執行“make b”時,bash 說:

cp abc.tex "abc-.tex"

答案1

在 Makefile 中,$(...)表示多字元變數的擴展make。您沒有make名為 的變量date +"%Y-%m-%-d-%H-%M-%S",因此它被替換為空字串。

make若要讓使用execute$(...)作為指令取代的shell ,請將其寫為$$(...)

b:
        cp abc.tex "abc-$$(date +"%Y-%m-%-d-%H-%M-%S").tex"

GNUmake變體make也具有$(shell ...)與 shell 中的命令替換類似的工作方式。

答案2

也許您正在尋找$(shell ...)巨集。

b:
    cp abc.tex "abc-$(shell date +"%Y-%m-%-d-%H-%M-%S").tex"

這會產生以下輸出

> make b
cp abc.tex "abc-2021-10-21-16-54-02.tex"

相關內容