我在 make
文件中有以下代码,它来自命令字符串数组
apps := $(shell fxt run apps)
go:
@for v in $(apps) ; do \
echo inside recipe loop with sh command: $$v ; \
done
命令返回类似[app1 app2 appN]
的数组(里面可能有很多应用)
但我需要它是 app1 app2 appN
,知道如何删除数组 []
吗?
当我运行 make 时,我得到了以下信息
inside recipe loop with sh command: [app
inside recipe loop with sh command: app2]
最佳答案
您可以简单地使用带有空替换部分的 subst
命令来删除部分变量内容,如下所示:
apps := $(shell ls)
#add []
apps := [ $(apps) ]
$(info Apps: $(apps))
# replace [ with nothing -> remove it
apps := $(subst [ ,,$(apps))
# replace ] with nothing -> remove it
apps := $(subst ],,$(apps))
$(info Apps: $(apps))
或使用 filer-out
:
apps :=$(filter-out [ ], $(apps))
取而代之的是两个替换函数。重要提示:在括号之间保留一个 空格
,因为 filter-out
需要一个单词列表。所以这里命令的模式部分有 2 个词。
输出:
Apps: [ a b c Makefile ]
Apps: a b c Makefile
如果输入的 [
和第一个单词之间没有空格,则必须使用 subst
命令。
也许您喜欢将两个表达式连接成一个(但可读性较差):
apps := $(subst ],,$(subst [,,$(apps)))
关于linux - 如何循环字符串并删除字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52308564/