makefile - accessing a variable inside foreach
问题描述
How can I access a variable from outside foreach loop . I want to print index number for the record. my code .
LIST=$(shell ls)
test:
i=0
@$(foreach k,$(LIST), echo "index $(i) = $k";)
Output desired is like below .
index 0 = A
index 1 = B
index 2 = C
index 3 = D
解决方案
食谱应该由 shell 命令组成,而不是 make-statements,例如:-
生成文件
LIST := $(shell ls) # This is a make-statement
test: # This is a target
@# And this is its recipe...
@# So write shell commands here...
@i=0; \
for f in $(LIST); do \
echo "index $$i = $$f"; \
i=$$(expr $$i + 1); \
done
或者简单地说:
test:
@i=0; \
for f in $$(ls); do \
echo "index $$i = $$f"; \
i=$$(expr $$i + 1); \
done
运行如下:
$ make
index 0 = A
index 1 = B
index 2 = C
index 3 = D
index 4 = Makefile
推荐阅读
- stackblitz - Stackblitz 开发服务器不工作
- tensorflow - 不同的tf版本对推理有影响
- xcode - `xcrun altool --upload-app` 在第一次尝试时没有响应,并在以下尝试时抛出错误
- excel - 如何修复 VBA 中的“编译错误:未定义用户定义的类型”
- three.js - Threejs更新从blender导出的模型的UV贴图
- python-3.x - 动态更改 f 字符串变量之一
- angular - 无法在活动和非活动Angular 8之间切换
- python - 如何检查加速度列中的峰值?
- vb.net - 连接尝试显示“输入有效的用户名和密码”
- python-3.x - 如何在 Python3.7 docker 容器中执行并行 PostgreSQL 查询?