[英]How to execute shell command
I have some trouble with execute shell commands from a Go program:我在从 Go 程序执行 shell 命令时遇到了一些问题:
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
如果
command
变量等于一个单词,如/home/slavik/project/build
(构建是 shell 脚本),它可以工作,但如果我尝试传递一些参数,即/home/slavik/project/build xxx
或/home/slavik/project/build -v=1
Go 程序引发异常,如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 :
查看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:因此,如果您想通过 eg
-v=1
,您的电话可能看起来像:
dateCmd := exec.Command(pwd + "/build", "-v=1")
用
exec.Command(pwd + "/build", fileName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.