【问题标题】:Dynamically replace variables to vars (makefile awk)将变量动态替换为 vars (makefile awk)
【发布时间】:2018-08-15 18:57:47
【问题描述】:

在我的 Makefile 中,我有一些食谱,例如

start-$(PARAM_NAME): ## Comment 1
do some things...
stop-$(PARAM_NAME): ## Comment 2
do some things

我想使用配方帮助,它可以解析文件并打印信息,例如

start-name    Starts...
stop-name     Stops

PARAM_NAME == 名称 我从具有 key=value 结构的配置文件(如 env 文件)中获得的所有 PARAM_NAME

Makefile 配方

help:
    @awk 'BEGIN {FS=":.*?## "} /^[A-z-]*(.\([A-z]*\))?[A-z-]*:.*?## / {printf "\033[36m-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

这与注释一致,分解为名称和注释,但名称中有 start-$(PARAM_NAME)。我想将 $(PARAM_NAME) 替换为其变量(名称)。

【问题讨论】:

  • 首先弄清楚如何在makefile之外做任何你想做的事情,然后弄清楚如何在makefile中调用那个脚本。顺便说一句,我不确定你认为":.*?## " 是什么意思,但不管它是什么意思,这并不意味着......

标签: regex awk makefile environment-variables


【解决方案1】:

如果您使用标准格式 $VARIABLE${VARIABLE} 的 shell 变量,则有一个命令将执行参数替换。

$ envsubst <file | awk -F':.*## ' -v OFS='\t' 'NF>1 {print $1,$2}'

start-name      Comment 1
stop-name       Comment 2

当然,如果打印的内容超出您的要求,请将 NF&gt;1 更改为某种过滤。添加颜色以增加风味... :)

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 2023-01-11
    • 2020-02-29
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多