云外壳终端中的 gcp bash 脚本

我想实现在我的组织中启用的 GCP API 的清单,所以我想在我的云 shell 终端中使用 bash 脚本。下面的代码有效,但是当我还想在数组中组织结果(使用列项目和 API)时,它不起作用。例如,当我将 gcloud 命令放入 bash 变量时,它不起作用: services=gcloud services list --enabled --project $project

#!/bin/bash
for project in  $(gcloud projects list --format="value(projectId)")
do
   echo "ProjectId:  $project"
   gcloud services list --enabled --project $project
done
stack overflow gcp bash script in cloud shell terminal
原文答案
author avatar

接受的答案

I have a solution with an additional nested loop

#!/bin/bash
  for project in  $(gcloud projects list --format="value(projectId)")
       do
         for api in $(gcloud services list --enabled --project $project --format='value(NAME)')
           do
             echo "${project},${api}"
           done 
       done

It's a CSV format, it should be easy to read it with Excel afterward.


答案: