1

I am using K8S

I want to calculate a string that that is a result of kubectl exec -it ... -c

after the -c option there is a string.

How can I pass a string with double quotes inside.

The following example doesn't work properly.

x="$(kubectl exec -it mysql-pod -- /bin/sh -c \"mysql -uroot -p12345
   -e 'show databases'\" 2>/dev/null)"
echo $x

Thanks.

Eitan
  • 1,286
  • 2
  • 16
  • 55
  • Can you `kubectl port-forward` to the pod, then run the `mysql` client locally? That would avoid multiple layers of quoting and escaping. – David Maze Mar 22 '22 at 23:33

1 Answers1

3
  • when only a command needs to be executed on a pod , -it option is not required as it stands for attaching an interactive teminal
  • when mysql is itself an executable command , no need to use /bin/sh -c
  • no need to encapsulate whole command in " "

So try following

x=$(kubectl exec mysql-pod -- mysql -uroot -p12345 -e 'show databases ;' 2>/dev/null)
echo $x 
confused genius
  • 2,876
  • 2
  • 16
  • 29
  • Yes, hreat. It works. BTW - if I need to add an aprostphe - how could I do that? i.e password with @ or any speciail character? I also may meed some command line to calculate that password – Eitan Mar 23 '22 at 04:27
  • you can add single quotes to password without any changes to whole command , it should work fine , x=$(kubectl exec mysql-pod -- mysql -uroot -p'12345' -e 'show databases ;' 2>/dev/null) – confused genius Mar 23 '22 at 04:39