简体   繁体   中英

How to execute shell command

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 ie /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?

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 eg -v=1 , your call probably should look something like:

dateCmd := exec.Command(pwd + "/build", "-v=1")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM