Geo*_*ler 3 string bash split makefile gnu-make
在 bash 中,我可以拆分一个字符串,类似于 How to split a string in shell and get the last field
foo=1:2:3:4:5
echo ${foo##*:}
Run Code Online (Sandbox Code Playgroud)
如何在 makefile 中使用 make 实现类似的效果?我发现的任何解决方案都比 bash 吊坠复杂得多。
我尝试使用:
$(shell ${foo##*:})但这失败了,因为字符串似乎没有正确终止。
foo是 make 变量\n\n\n\n
$(subst from,to,text)对文本\n text执行文本替换:每次出现的from都被替换为to。结果将替换函数调用。
所以:
\n\n$(subst :, ,$(foo))\nRun Code Online (Sandbox Code Playgroud)\n\n通过用空格foo替换所有内容来分割 make 变量的内容。:仍来自 GNU make 手册:
\n\n\n\n
$(lastword names\xe2\x80\xa6)参数名称被视为一系列由空格分隔的名称。该值是系列中的\n 姓氏。
所以:
\n\n$(lastword $(subst :, ,$(foo)))\nRun Code Online (Sandbox Code Playgroud)\n\n应该做你想做的事。演示(host>是shell提示符):
host> cat Makefile\nfoo := 1:2:3:4:5\n\nall:\n $(info $(lastword $(subst :, ,$(foo))))\nhost> make\n5\nRun Code Online (Sandbox Code Playgroud)\n\nfoo是 shell 变量你必须:
\n\n$在将配方传递到 shell 之前,保护配方的标志免受 make 执行的第一次扩展的影响,all:\n @foo=1:2:3:4:5 && \\\n echo $${foo##*:}\nRun Code Online (Sandbox Code Playgroud)\n\n应该做你想做的事:$符号被正确转义,并且它是一个单行配方(感谢尾随\\)。演示:
host> cat Makefile\nall:\n @foo=1:2:3:4:5 && \\\n echo $${foo##*:}\nhost> make\n5\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5365 次 |
| 最近记录: |