我试图使用exec.Cmd(command, flags...)运行命令,并希望在调用cmd.Run()函数之前具有修改参数的灵活性。
例如:
cmd := exec.Command("echo", "hello world")
cmd.Env = []string{"env1=1"}
cmd.Args = []string{"echo2", "oh wait I changed my mind"}
cmd.Run()上面的代码似乎总是运行echo hello world,而不是echo2 oh wait I changed my mind。
我是否正确地期望运行echo2而不是echo?
发布于 2017-12-06 06:43:02
当将命令更改为执行时,还必须像在cmd.Path中一样设置exec.Command。
cmd := exec.Command("echo", "hello world")
cmd.Env = []string{"env1=1"}
cmd.Args = []string{"echo2", "oh wait I changed my mind"}
lp, err := exec.LookPath("echo2")
if err != nil {
// handle error
}
cmd.Path = lp
if err := cmd.Run(); err != nil {
// handle error
}https://stackoverflow.com/questions/47667479
复制相似问题