在 Makefile 中運行任何目標之前,如何設定變數 - 來自文本檔案的鍵/值對?
假設文本.p4config檔案包含以下內容:
P4PORT=ilp4.il.domain.com:1667 P4CLIENT=ws:domain.com::user:container.IP_V1:a0a12fc1
在 makefile 中這是行不通的:
export $(cat .p4config | grep P4CLIENT | xargs) test: env > env.log
期望在 env.log 中看到變數 P4PORT 和 P4CLIENT
cat .p4config | grep P4CLIENT | xargs
結果:
P4PORT=ilp4.il.domain.com:1667 P4CLIENT=ws:domain.com::user:container.IP_V1:a0a12fc1
但是在 makefile 中添加“export”并不能解決問題
uj5u.com熱心網友回復:
我想知道這是否可以達到您的預期:
.ONESHELL:
all:
eval "$$(perl -ne 'if (/P4CLIENT/) { map {print "export $$_\n"} split }' .p4config)"
env | grep P4
的輸出env | grep P4:
P4CLIENT=ws:domain.com::user:container.IP_V1:a0a12fc1
P4PORT=ilp4.il.domain.com:1667
uj5u.com熱心網友回復:
我不確定你為什么認為那會起作用。makefile 不是 shell 腳本,因此您不能只在其中撰寫一堆 shell 操作并期望以相同的方式運行。手冊中描述了 makefile 的語法。
生成檔案可以包含 shell 腳本,但只能包含在配方中。因此,您可以將在命令列鍵入的相同命令放入配方中:
env.log:
cat .p4config | grep P4CLIENT | xargs > $@
現在,如果您鍵入make env.log(或列為先決條件的任何其他目標env.log),make 將為您創建該檔案(當然,請注意此處的縮進必須是真正的 TAB 字符,而不僅僅是空格)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536828.html
標籤:Linux狂欢生成文件
上一篇:獲取diff并用awk替換字串
