0
pods=$(kubectl get pods  | awk '/e2e-test/ && !/$HOSTNAME/')

I would like to filter and retrieve all pods in the kubectl result which start in "e2e-test", but which are different from the hostname. I tried different ways but they all seem ignore the variable.

netik
  • 1,736
  • 4
  • 22
  • 45
  • 1
    Could you please post sample output of `kubectl get pods` and what is expected output too in your question, for more clarity. – RavinderSingh13 Nov 09 '20 at 19:31
  • Does ["How do I use shell variables in an awk script?"](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) answer your question? – Gordon Davisson Nov 09 '20 at 19:40

2 Answers2

3

Single quotes ' don't expand variables, double quotes " do. This should work:

pods=$(kubectl get pods  | awk "/e2e-test/ && !/$HOSTNAME/")

On the other hand: You are using awk only to filter lines here, so this might be easier to read and maintain:

pods=$(kubectl get pods  | grep 'e2e-test' | grep -v "$HOSTNAME")
Kaligule
  • 630
  • 3
  • 15
1

The reason bash fails expand $HOSTNAME variable is because it is quoted inside ' quotes.

This is working as design.

The simplest dirty trick is to exclude the $HOSTNAME variable from the quote by replacing it with '$HOSTNAME' , like this:

pods=$(kubectl get pods  | awk '/e2e-test/ && !/'$HOSTNAME'/')
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30