shell - 如何在 Makefile 中正确编写 for 循环
问题描述
我浏览了一些帖子,但仍然不知道它是如何工作的。
我的要求是:
for i in *.json
do
file = `echo $i |cut -d _ -f2`
echo ${file}
# do the rest tasks
done
如何将上述脚本转换为 Makefile 的目标?
这是我尝试过的
foo:
for i in *.json; do \
$(eval FILE = $(shell echo $$i |cut -d _ -f2)); \
echo $(FILE) ;\
done
但它不起作用
解决方案
Using $(eval)
or $(shell)
is ... not even wrong.
foo:
for i in *.json; do \
file=$$(echo "$$i" |cut -d _ -f2); \
echo "$$file"; \
done
Notice the quoting of the filename variables, and the absence of spaces around the =
assignment operator, and the doubling of any dollar sign in order to pass it through from make
to the shell.
However, the shell provides a much better mechanism for this;
foo:
for i in *.json; do \
j=$${i#*_}; \
echo "$${j%%_*}"; \
done
or perhaps
foo:
printf '%s\n' *.json \
| sed 's/[^_]*_\([^_]*\)_.*/\1/'
If you only expect a single underscore, both of these can be further simplified.
Or maybe you are just looking for
makefile_variable := $(foreach x,$(wildcard *.json),$(word 2,$(subst _, ,$x)))
推荐阅读
- c# - 如何正确使用 asp.net razor-pages 中的部分视图?
- r - 使用ggplot2在叠加密度图中重复图例
- scala - Scala akka 键入:如何从它的实例中获取 ActorRef 到 Actor 并自己发送消息?
- delphi - 如果程序未提升,OpenProcess() 返回 ERROR_ACCESS_DENIED
- java - Hbox 有子 Buttons ,如何抓取和添加监听器?
- android - Android openGL ES 3.0 着色器不工作
- cloudconvert - API CloudConvert curl 请求 PHP
- authentication - 无法授予 MS Asure 图形 API 客户端应用程序获取有关 ManagedDevices 的数据的权限。如何克服?
- sql-server - Subshell 中的 Bash BG 进程:显示输出以及管道到命令(Docker CMD 启动 Shell 脚本)
- python - 尝试在某个日期范围内抓取 google 的结果数量错误