2

Ok, there are similar questions, but not exactly what I am looking for.

I am working with Kubernetes, so the information is from that, but this question is for Shell Scripting, so please don't push me off to Kubernetes people. :)

I run the following command to get the information:

kubectl get pods -o wide --all-namespaces | grep sonarqube-

Result Example:

default       sonarqube-664b4fd48-g6nvb                   1/1       Running   0          4d        10.233.64.6     local-node-0

Goal: What I want is to take the first 2 values (default and sonarqube-664b4fd48-g6nvb) and turn them into variables that I can use elsewhere in the script.

Command Sample I want to use the variables in is like:

kubectl cp <file> ${namespace}/${deployment}:/opt/app/extensions/plugins/

Thanks, and please let me know if more information is needed.

2

generally

What will work in bash and in other shells is to set positional parameters from the output of the pipe:

set -f -- $(kubectl get pods -o wide --all-namespaces | grep sonarqube- )

In this case, you can now access the variables as $1, $2, etc.

specifc for bash

In bash it is possible to create an array variable, like so:

var=( $(kubectl get pods -o wide --all-namespaces | grep sonarqube- ) )

The idiom is var=( ... ), in this case the output of your grep pipe.

You can then access elements of your array using the form ${var[n]}, with n starting at zero.

  • This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess). – DC.Skells Mar 6 '18 at 21:46
  • @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark. – user1404316 Mar 6 '18 at 21:49
  • 2
    As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk. – ilkkachu Mar 6 '18 at 21:57
  • @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks. – user1404316 Mar 6 '18 at 22:08
  • @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :) – DC.Skells Mar 6 '18 at 22:37
3

With bash's read function:

read -r namespace deployment other < <(kubectl get pods -o wide --all-namespaces | grep sonarqube-)
kubectl cp <file> "${namespace}/${deployment}:/opt/app/extensions/plugins/"
  • Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion. – John1024 Mar 6 '18 at 21:46
0

In cases like this (with predictable output columns), I usually reach for awk. Something like this might be useful:

kubectl get pods -o wide --all-namespaces | awk '/sonarqube-/ { cmd=sprintf("kubectl cp <file> %s/%s:/opt/app/extensions/plugins", $1, $2); print(cmd); system(cmd); }' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.