How to loop on strings and remove the string array

2019-08-23 04:39发布

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条回答
爷、活的狠高调
2楼-- · 2019-08-23 05:35

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)))
查看更多
登录 后发表回答