bashopenshiftherestring

oc exec with heredoc: cannot pass variable into command to execute in container; rpc error: code = 2 desc = oci runtime error


I want to use oc exec to execute a cat in my pod. In this cat command I need to expand some variable.

cat /opt/amq/data/split-$index/running

So, I try this:

oc exec -i $pod -- '"/bin/bash" -s <<EOF cat /opt/amq/data/split-$index/running EOF'

But I cannot get it working.

rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:235: starting container process caused "exec: \"\\\"/bin/bash\\\" -s <<-EOF cat /opt/amq/data/split-$index/running EOF\": stat \"/bin/bash\" -s <<-EOF cat /opt/amq/data/split-$index/running EOF: no such file or directory"

I can put cat and bash in separate lines, but the error is the same: $index not expanded.

                    oc exec -i $pod -- '"/bin/bash" -c <<EOF
                    cat /opt/amq/data/split-$index/running
                    EOF'

Error:

rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:235: starting container process caused "exec: \"\\\"/bin/bash\\\" -c <<EOF\\n                    cat /opt/amq/data/split-$index/running\\n                    EOF\": stat \"/bin/bash\" -c <<EOF\n                    cat /opt/amq/data/split-$index/running\n                    EOF: no such file or directory"

Solution

  • Find a way without using heredoc.

    oc rsh can run commands and return results directly:

    oc rsh pod_name cat /opt/data/...
    

    So that's what I need. Then I don't need oc exec.

    And, see the error message, it says:

    stat \"/bin/bash\" -s <<-EOF cat /opt/amq/data/split-$index/running EOF: no such file or directory
    

    Notice the stat, it seems to be looking for a file.