How to use 'kubectl cp' with bash variables

5/22/2019

I trying to automate copying files from a kubernetes pod with a shell script. I am getting unexpected errors while using shell variables.

I am able to populate shell variables using the following code

➜  ~ read -r namespace pod other < <(kubectl get pods -o wide --all-namespaces | grep control- )
➜  ~ echo $namespace
mynamespace
➜  ~ echo $pod
control-7dbdb8fff8-8c548
➜  ~ read -r file other < <(kubectl exec -it $pod -n=$namespace -c my-sidecar -- ls | grep control)
➜  ~ echo $file
control-7dbdb8fff8-8c548_20190522_192354.dat

When I try to combine these variables into a kubectl cp command I get an error

➜  ~ kubectl cp $namespace/$pod:$file -c my-sidecar ~/
: No such file or directory48_20190522_192354.dat
tar: error exit delayed from previous errors
 no such file or directory8c548_20190522_192354.dat
-- John Rodgers
bash
grep
kubectl
kubernetes
shell

0 Answers