如何调试“退出状态1”在Golang中运行exec.Command时出错 [英] How to debug "exit status 1" error when running exec.Command in Golang
本文介绍了如何调试“退出状态1”在Golang中运行exec.Command时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行下面的代码:
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())
退出状态1
exit status 1
如何获取更详细的信息?
How to get more detailed information?
推荐答案
解决方案是使用Command对象的 Stderr
属性。这可以这样做:
The solution is to use the Stderr
property of the Command object. This can be done like this:
cmd := exec.Command("find", "/", "-maxdepth", "1", "-exec", "wc", "-c", "{}", "\\")
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())
运行上面的代码,会清楚问题是什么:
Running the above code, would make it clear what the issue is:
退出状态1:find:-exec:no terminated;或+
exit status 1: find: -exec: no terminating ";" or "+"
这篇关于如何调试“退出状态1”在Golang中运行exec.Command时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文