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

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

我想使用 oc exec 在我的 pod 中执行 cat。在这个 cat 命令中,我需要扩展一些变量。

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

所以,我试试这个:

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

但是我无法让它工作。

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"

我可以把 c​​at 和 bash 放在不同的行,但是错误是一样的:$index not expanded.

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

错误:

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"

找到不使用 heredoc 的方法。

oc rsh 可以直接 运行 命令和 return 结果:

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

这就是我需要的。那我就不需要oc exec.

而且,看到错误消息,它说:

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

注意 stat,它似乎在寻找文件。