1

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.

codec
  • 7,978
  • 26
  • 71
  • 127
  • 2
    You should surround your whole bash command with single quotes rather than double quotes: `'err=$(tar -cvzf /tmp/logs_aj.tgz ${all_files} 2>&1) || ( export ret=$?; [[ $err == *"No such file or directory"* ]] || exit "$ret" )'` – Martin Tovmassian Dec 12 '22 at 10:15

1 Answers1

2

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" )'
mar0ne
  • 148
  • 1
  • 10