How to loop on strings and remove the string array

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:25:42

问题


I've the following code inside make file which gets from command array of strings

apps := $(shell fxt run apps)


go:
    @for v in $(apps) ; do \
        echo inside recipe loop with sh command: $$v ; \
    done

The command returns array like [app1 app2 appN] (could be many apps inside) But I need it to be app1 app2 appN , any idea how to remove the array [] ?

when I run make I got the following

inside recipe loop with sh command: [app
inside recipe loop with sh command: app2]

回答1:


You simple can use subst command with empty replacement part to remove parts of your variable content like this:

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))

or using filer-out with:

apps :=$(filter-out [ ], $(apps))

Instead the two replacement functions. Important: Keep a space between the brakets, as filter-out needs a list of words. So you have 2 words in the pattern part of the command here. Output:

Apps: [ a b c Makefile ]
Apps: a b c Makefile

If the input has no space between [ and the first word, you have to use the subst command.

Maybe you like to concatenate both expressions in one ( but less readable ):

    apps := $(subst ],,$(subst [,,$(apps)))


来源:https://stackoverflow.com/questions/52308564/how-to-loop-on-strings-and-remove-the-string-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!