douya2982
2017-05-14 04:09
浏览 259
已采纳

直接运行bash命令和通过exec.Command运行结果不同

On Linux OS, run "realpath ~/bin" gives the correct path "/home/user1/bin".

But when I run it as bellow:

cmd := exec.Command("realpath", "~/bin")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
    panic(err)
}
fmt.Println("Realapth:", out.String())

I got panic: exit status 1.

BTW: with command as below:

cmd := exec.Command("realpath", "--help")

I can get correct help message for realpath command. I think that means it is really in my executable path.

1条回答 默认 最新

相关推荐 更多相似问题