当我运行下面的代码:
cmd := exec.Command("find","/","-maxdepth","1","-exec","wc","-c","{}","\\") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { fmt.Println(err) return } fmt.Println("Result: " + out.String())
我收到这个错误:
exit status 1
但是,这对调试错误的确切原因没有帮助.
如何获取更详细的信息?
解决方案是使用Command对象的Stderr属性.这可以这样做:
cmd := exec.Command("find","\\") var out bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &out cmd.Stderr = &stderr err := cmd.Run() if err != nil { fmt.Println(fmt.Sprint(err) + ": " + stderr.String()) return } fmt.Println("Result: " + out.String())
运行上面的代码,会说明问题是什么:
exit status 1: find: -exec: no terminating “;” or “+”