dongzhan7909 2018-09-12 19:32
浏览 613
已采纳

如何在字符串数组上循环

I've command line tool which implemented with golang, which works OK. I want to execute some command that should provide list of strings

apps := $(shell fxt run apps)
apps:
    @echo $(apps) is called

in the terminal I see the following while executing make (which is totally fine)

[app1 app2] is called

Since the command fxt run apps return array of string (var apps []string)

My question is how can I loop over the apps variable?

the data which is returned by the command is fine, but now I need to take this list (app1... appN) and loop over it, the issue which is not clear to me, how can I loop over array of strings?

special case is if in the list of theloop I got app7 how should do a fork in the code for example if(app7) prints mvn clean install

example.

for each app (in the app list) I need to run command

go test ./...

But for app7 in need to run

mvn clean install

and for app10

yarn

  • 写回答

1条回答 默认 最新

  • doushi9376 2018-09-12 20:32
    关注

    You want to run your loop inside make itself or in the recipe which is in fact executing a shell? Here you have both!

    Remark: I replace the executed command to test it myself. Here I use ls to fill my array.

    apps := $(shell ls)
    
    #looping in make itself
    $(foreach var,$(apps),$(info In the loop running with make: $(var)))
    
    #loop in shell inside recipe 
    go:
        @for v in $(apps) ; do \
            echo inside recipe loop with sh command: $$v ; \
        done
    

    Output:

    In the loop running with make: a
    In the loop running with make: b
    In the loop running with make: c
    In the loop running with make: Makefile
    inside recipe loop with sh command: a
    inside recipe loop with sh command: b
    inside recipe loop with sh command: c
    inside recipe loop with sh command: Makefile
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退