Django
Django

Reputation: 143

gcloud command output formatting to use results in another gcloud command

I'm trying to automate the deletion of SSL certificates that end with a certain text pattern on GCP projects.

For this I use the command:

 gcloud compute ssl-certificates list --filter="name~'819$'" --format="(name)"

Which output displays exactly this format:

NAME
certname1-1602160819
certname2-1602160819
certname3-1602160819
...and so on

The thing is that if I want to use the results from this command to then use it to input another gcloud command that deletes each certificate, I get the first variable as NAME which is the field title and obviously not a certificate.

Here is my script:

#!/bin/bash
for oldcert in $( gcloud compute ssl-certificates list --filter="name~'819$'" --format="(NAME)")
do
    gcloud compute ssl-certificates delete $oldcert
done

Do you know how I could get the field name NAME out of my output so I could treat each result in another command directly.

Thanks for your precious advices

Upvotes: 1

Views: 4577

Answers (2)

Jofre
Jofre

Reputation: 3898

The right format to use is --format="value[](name)".

According to the docs:

value

CSV with no heading and <TAB> separator instead of <COMMA>. Used to retrieve individual resource values.

So it's equivalent to the --format="csv[no-heading](name)" that you used, but "more correct" (and a little more legible).

Upvotes: 5

Django
Django

Reputation: 143

@Hitobat thanks very much for your comment

I used the csv[no-heading] option even though the tails -n +2 otion also does the job

the following commands did the job great:

#!/bin/bash
for oldcert in $( gcloud compute ssl-certificates list --filter="name~'819$'" --format="csv[no-heading](name)")
do
    gcloud compute ssl-certificates delete $oldcert --quiet
done

Upvotes: 2

Related Questions