1

I have some trouble with execute shell commands from a Go program:

  var command = pwd + "/build " + file_name

  dateCmd := exec.Command(string(command))
  dateOut, err := dateCmd.Output()
  check(err)

If command variable equals a single word like /home/slavik/project/build (build is shell script) it works, but if I try to pass some arg i.e. /home/slavik/project/build xxx or /home/slavik/project/build -v=1 the Go program raises an exception like file /home/slavik/project/build not found

What's wrong with my code?

1

2 Answers 2

9

You have to pass the program and the arguments separately. See the signature of exec.Command:

func Command(name string, arg ...string) *Cmd

So if you want to pass e.g. -v=1, your call probably should look something like:

dateCmd := exec.Command(pwd + "/build", "-v=1")
Sign up to request clarification or add additional context in comments.

Comments

4

Use

exec.Command(pwd + "/build", fileName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.