makefile - 使用 makefile 阅读和跳过注释行的更好方法
问题描述
此代码片段适用于“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)
有两个问题:
有兴趣知道在 make 的内置函数的帮助下是否有更好的方法来实现相同的目标。
运行“make -s -f Makefile”会清除“dirlist.txt”中给出的所需目录。但是,echo 语句(在 else 块中)的输出也会打印带有注释的行,这些注释在给出的条件中被跳过'如果'块。我希望输出只包含要清除的那些目录名,这与预期的一样。
为什么是这样?
谢谢,维吉
解决方案
Make 用于“制作”文件,而不是删除它们,所以不要对这种漂亮的东西抱有希望。
GNU Make (4.2) 具有file
读取文件的功能,请参阅此答案:
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 。
推荐阅读
- python - Pyspark python reduceByKey 过滤器由 math.max
- java - 使用 formatter.parse() 时收到错误为不可解析日期:“”
- python - AttributeError:“元组”对象没有属性“编码”python
- android - 反应没有在android中运行
- android - Android Studio:错误:程序类型已存在:androidx.core.graphics.PathSegment
- java - Spring Boot 安全性中的数据库身份验证
- java - 在java中使用这个关键字和同步块
- python - 如何从不使用 POST 的网站上抓取信息
- html - 仅使用 iOS API 从 html 中提取 JSON 字符串
- ios - iOS RxSwift如何检查Result == .success?