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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛