0

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"
WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • Hi @user1934428 I break the lines but still error. `$index` is not expanded, equals to empty str. – WesternGun Mar 15 '22 at 12:43
  • You have a single quote after the `EOF`. – user1934428 Mar 15 '22 at 13:30
  • Because the first quote is single quote, after `--`. – WesternGun Mar 15 '22 at 13:51
  • This means that you have no newline after the last line of the HERE document. I don't know how forgiving bash is, but technically speaking, this is not correct. BTW, is there a reason why you use a HERE-doc for just submitting a single `cat`? – user1934428 Mar 15 '22 at 15:41
  • BTW, you did not show how you define `index`, but `$index` won't be expanded by the invoking shell due to your single quotes. Perhaps it will be expanded if you put it into the environment (to answer this, I would need to know more about _openshift_). – user1934428 Mar 15 '22 at 15:45
  • Yeah. Maybe u r right, no need to heredoc for cat. I found a solution to it, using rsh but not exec. See below. – WesternGun Mar 16 '22 at 08:35

1 Answers1

0

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.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • _it seems to be looking for a file_ : Because in your original attempt, you had the construct `os exec .... 'long string between quotes'`, and the whole string was there fore taken as command to be executed. The general syntax is `os exec COMMAND ARG1 ...`, and you provided, syntactically, only COMMAND and no arguments. See [here](https://docs.openshift.com/container-platform/3.11/dev_guide/executing_remote_commands.html). – user1934428 Mar 16 '22 at 08:47
  • Ah. Thanks. Good doc. So what if a command does not need args? – WesternGun Mar 16 '22 at 11:37
  • Then you don't provide any :-)))) – user1934428 Mar 16 '22 at 12:20