[英]How to execute shell command
我在从 Go 程序执行 shell 命令时遇到了一些问题:
var command = pwd + "/build " + file_name
dateCmd := exec.Command(string(command))
dateOut, err := dateCmd.Output()
check(err)
如果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
我的代码有什么问题?
您必须分别传递程序和参数。 查看exec.Command的签名:
func Command(name string, arg ...string) *Cmd
因此,如果您想通过 eg -v=1
,您的电话可能看起来像:
dateCmd := exec.Command(pwd + "/build", "-v=1")
用
exec.Command(pwd + "/build", fileName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.