【问题标题】:how to use if statement from a makefile function?如何使用 makefile 函数中的 if 语句?
【发布时间】:2019-04-22 16:24:26
【问题描述】:

我正在从 .config 文件中读取配置,如果启用了配置,我想做一些操作。我编写了以下函数,但它抛出错误消息“/bin/sh: 1: Syntax error:”)”意外(期待“then”)

define parse_configs
    while read -r file; do \
        config=$$(echo $$file | grep -Po '(?<=(CONFIG_)).*(?==)'); \
        val=$$(echo $$file | grep -Po '(?<=(=)).*'); \
        $$(if $(findstring y, $$val), echo "do Ops", echo "No ops"); \
    done < .config;
endef

问题在于 if 语句,函数的其他部分很好。请让我知道代码中的错误。谢谢。

【问题讨论】:

  • 呃,有很多更好的方法可以做到这一点...... :'( 你能展示你的 .config 吗?
  • 你找错地方了——Make没有抱怨那个特定的if声明——Make不使用then的,所以它不会期望一个.您应该发布如何调用宏,因为那里可能存在导致您的问题的东西。不过,上面代码中的一件事——您从 $(findstring y, $$val) 引用 $$val。因为只有一个$,所以findstring 将在第一次读取宏主体时展开,因此在字符串文字$val 中查找y(每次都不会返回任何内容)。
  • Make 将在分配 val 之前尝试扩展 $(findstring y, $$val)。这将如何运作?

标签: bash makefile gnu-make


【解决方案1】:

语句有什么问题:

$$(if $(findstring y, $$val), echo "do Ops", echo "No ops");

那实际上是一个GNU Make if-function, 拨打GNU Make findstring-function, 您在 shell 语句的中间编写了它,并要求 ($$) 它由 shell 扩展,但它对 shell 没有意义。 它也可能是Javascript。将其替换为适当的 shell if 语句,例如

while read -r file; do \
    config=$$(echo $$file | grep -Po '(?<=(CONFIG_)).*(?==)'); \
    val=$$(echo $$file | grep -Po '(?<=(=)).*'); \
    if [ -z $${val##*"y"*} ]; then echo "do Ops"; else echo "No ops"; fi; \
done < .config;

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2019-08-30
    • 2020-02-24
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多