【问题标题】:Better way to read and skip commented lines using makefile使用 makefile 阅读和跳过注释行的更好方法
【发布时间】:2020-07-02 08:36:13
【问题描述】:

此代码 sn-p 适用于 'dirlist.txt' 中的给定目录列表

DIR_LIST = "dirlist.txt"

exclude_legacy:
    @echo "=== Checking dirs to exclude..."
    while read -r dirto_exclude; \
    do \
        if [[ "$${dirto_exclude}" = \#.* ]]; then \
            continue; \
        else \
            echo "Exclude this dir, $${dirto_exclude}"; \
            rm -rf $${dirto_exclude}; \
        fi \
    done < $(DIR_LIST)

有两个问题:

  1. 有兴趣了解是否有更好的方法可以借助 make 的内置函数实现相同的功能。

  2. 运行“make -s -f Makefile”会清除“dirlist.txt”中给出的所需目录。但是,echo 语句(在 else 块中)的输出也会打印带有 cmets 的行以及在条件中被跳过的行在“if”块中给出。我希望输出仅包含要清除的那些目录名,这与预期的一样。

这是为什么?

谢谢, 维吉

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    Make 用于“制作”文件,而不是删除它们,所以不要对这种漂亮的东西抱有希望。

    GNU Make (4.2) 具有用于读取文件的file 函数,请参阅this answer

    DIR_LIST := $(file < dirlist.txt)
    

    看起来您在前导“#”上有一些类似注释的语法。在那种情况下,我会尝试使用 grep(未测试):

    DIR_LIST := $(shell grep -v "#*" dirlist.txt)
    

    接下来我们可以使用rm -rf 在一次调用中删除所有文件:

    exclude_legacy:
        @echo Removing directories "$(DIR_LIST)"
        rm -rf $(DIR_LIST)
    

    如果DIR_LIST 变为空,rm 命令将失败。如果你需要它来工作,我会试试 xargs。

    【讨论】:

    • gmtt 有一个功能:github.com/markpiffer/…
    • @Andreas,同意您对使用 make 的评论。自从发布我的问题以来,我已经改变了摆脱注释行的方式 - 而不是 Makefile,现在在外部 shell 脚本中处理。
    • @vijayanmr 是的,制作(shell)脚本通常是最好的解决方案
    猜你喜欢
    • 2012-06-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    相关资源
    最近更新 更多