codec
codec

Reputation: 8836

Kubectl exec running a bash command with special character

I am trying to run the following command on a pod from my local machine.

kubectl -n myns exec mypod -- /bin/bash -c "err=$(tar -cvzf /tmp/logs_aj.tgz ${all_files} 2>&1) || ( export ret=$?; [[ $err == *"No such file or directory"* ]] || exit "$ret" )"

The command stores the output of the command in a variable and if the output contains a "No such file or directory" it ignores it else exits with the return code.

The command itself: err=$(tar -cvzf /tmp/bludr-logs_aj.tgz ${all_files} 2>&1) || ( export ret=$?; [[ $err == *"No such file or directory"* ]] || exit "$ret" ) runs fine when I run this manually inside the pod. But when I try to run this remotely using exec it gives it won't accept *. Here is what I get:

root:~# kubectl -n myns exec mypod -- /bin/bash -c "err=$(tar -cvzf /tmp/logs_aj.tgz ${all_files} 2>&1) || ( export ret=$?; [[ $err == *"No such file or directory"* ]] || exit "$ret" )"
such: Cowardly: command not found
such: -c: line 1: conditional binary operator expected
such: -c: line 1: syntax error near `*No'
such: -c: line 1: `Try 'tar --help' or 'tar --usage' for more information. || ( export ret=2; [[  == *No'
command terminated with exit code 1

I tried to replace * with &2A but that did not work.

Upvotes: 1

Views: 1099

Answers (1)

mar0ne
mar0ne

Reputation: 218

Your command contain nested double quotes so use single quotes instead:

kubectl -n myns exec mypod -- /bin/bash -c 'err=$(tar -cvzf /tmp/logs_aj.tgz ${all_files} 2>&1) || ( export ret=$?; [[ $err == *"No such file or directory"* ]] || exit "$ret" )'

Upvotes: 2

Related Questions