Sam-Tahir
Sam-Tahir

Reputation: 191

How to parse output from a kubectl command into an array for a bash script

I have this kubectl command which gives me the following output. I need to know how to store the output in an array, so I can loop through each of those and further issue gcloud commands on the resources.

kubectl get ingress test-load-balancer -o jsonpath='{.metadata.annotations.ingress\.kubernetes\.io/backends}'| jq  -r 'keys'

[
  "k8s-be-30423--12305b0f030d5a48",
  "k8s-be-31245--10023b0f030d5a48"
]

However, now I need to run individual commands for these k8s-xx resources inside a bash script. I tried the following but it doesn't save the output in an array

read BACKEND_SERVICES<<<$(kubectl get ingress cysiv-load-balancer -o \

jsonpath='{.metadata.annotations.ingress\.kubernetes\.io/backends}'| jq -r 'keys')

Thank you in advance

Upvotes: 0

Views: 4058

Answers (1)

Cyrus
Cyrus

Reputation: 88829

I assume that your keys does not contain spaces.

array=($(kubectl ... | jq -r 'keys[]'))
declare -p array

Upvotes: 1

Related Questions