0

I'm trying to pass a json data to jq command and replace a value like sed. But it's just printing the command and not executing it.

jsonStr, _ := json.Marshal(p.Values)
op, _ := exec.Command("echo", string(jsonStr), "| jq '.aws=1'").CombinedOutput()
fmt.Println(string(op))

It's just printing the value of p.Values which is a json and | jq '.aws=1' instead of replacing the aws key in p.Values and printing it.

The output is something like this.

{"name": "n1"} | jq '.aws=1'

But if I execute the same command from terminal, it's working fine and replacing the text in json.

Underoos
  • 4,708
  • 8
  • 42
  • 85
  • 1
    `echo` will print it's arguments. – colm.anseo Jun 17 '21 at 16:51
  • 1
    You are not executing a shell, so it is not going to interpret the arguments as a shell would. – JimB Jun 17 '21 at 16:52
  • a command-line shell like `bash` will interpret the `|` as a pipe and know to run one command, relaying its `stdout` to the next command's `stdin`. In your code snippet you are running a single command `echo` with 2 arguments - that are simply printed. – colm.anseo Jun 17 '21 at 16:58
  • 2
    You cannot use pipelining `|` in exec.Command, pipelining is a shell feature and the go `exec` package does not invoke a shell. You need to run a second `exec.Command` and set it's `Stdin` to the value of the first's `Stdout`. – Bracken Jun 17 '21 at 16:59

0 Answers0