0

Trying to expand a for loop variable in this does not succeed - I am trying to use the $i variable in the jsonpath for loop below:

for i in {0..9}; do 
    echo $i
    kubectl exec -i -t "$(kubectl get pod -l "app=mdm-shard" -o jsonpath='{.items[{$i}].metadata.name}')" -- cat /proc/net/udp
done

I get:

0
error: error parsing jsonpath {.items[{$i}].metadata.name}, invalid array index {$i}
error: pod name must be specified

I tried a lot of combinations but can't find the one that is going to expand $i inside the query.

My bash version:

GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
MaurGi
  • 1,698
  • 2
  • 18
  • 28
  • 3
    Single quotes suppress parameter expansion, use `jsonpath="{...}"` instead. – Benjamin W. Feb 20 '19 at 19:46
  • 2
    And probably `$i` instead of `{$i}`. – Benjamin W. Feb 20 '19 at 19:47
  • You can see there's something wrong with the quoting even here with the syntax highlighting. You need to be much more careful with the quotes;) – liborm Feb 20 '19 at 19:47
  • See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Feb 20 '19 at 19:51
  • 1
    @liborm Care to elaborate? The syntax highlighting is just due to the fact that the highlighter doesn't know that `$(...)` starts a new quoting context. – chepner Feb 20 '19 at 19:51
  • You perhaps want to use `jq --arg i "${i}"`, then reference `$i` inside your _single-quoted_ JQ directive. – bishop Feb 20 '19 at 19:56
  • @chepner neither I knew ;) But substituting `'` with `"` is really enough, as @Benjamin suggested. – liborm Feb 20 '19 at 20:01

1 Answers1

0

Thank you Benjamin - yes this worked:

for i in {0..9}; do 
  echo $i
  kubectl exec -i -t "$(kubectl get pod -l "app=mdm-shard" -o jsonpath="{.items[$i].metadata.name}")" -- cat /proc/net/udp; 
done
MaurGi
  • 1,698
  • 2
  • 18
  • 28