【问题标题】:How to add etire lines of a shell command output to Makefile define?如何将 shell 命令输出的整行添加到 Makefile 定义中?
【发布时间】:2022-11-30 23:07:13
【问题描述】:

我写在Makefile

define deploy_meta
$(shell git log -n 2 --oneline | awk '{print "commit"NR ": " $0}')
commit: nogit-$(timestamp)
tag: nogit-$(timestamp)
deployed-from: $(shell hostname)
deployed-by: $(USER)
deploy-date: $(shell date -u '+%Y%m%d%H%M%S')
endef

但如果给我

$cat .deploy    
commit1:  commit2: 
commit: nogit-1669806282
tag: nogit-1669806282
...

命令本身

git log -n 2 --oneline | awk '{print "commit"NR ": " $0}'

工作正常并给出两行。很明显,make 感觉到了,因为它打印了两个“commit#”字样。但它不打印内容。为什么?

【问题讨论】:

  • 您命令中的$0 将由make 解释。通过将 $ 加倍来逃避它: ... $$0 ....

标签: makefile gnu-make


【解决方案1】:

主要问题很简单:Make 在这里扩展$0(什么都没有)。你需要$$0

不过,如果你想要两行,$(shell) is also part of your problem here

shell 函数提供了与反引号 ('`') 在大多数 shell 中提供的相同功能:它确实命令扩展.这意味着它将 shell 命令作为参数并扩展为命令的输出。 make 对结果所做的唯一处理是将每个换行符(或回车/换行符对)转换为单个空格.如果有尾随(回车和)换行符,它将被简单地删除。

(强调我的)。您将需要一种完全不同的方法,例如,使用 $(shell) 生成一个文件,然后使用 include 该文件。

【讨论】:

    猜你喜欢
    • 2016-06-20
    • 2021-12-31
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2011-10-31
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多