【发布时间】:2019-01-09 22:59:51
【问题描述】:
我试图弄清楚为什么某些 shell 命令可以与 goloang exec.Command 一起使用,而其他命令在终端输入时都返回相同的结果时不能。我基本上想对 windows 和 mac 二进制文件使用相同的命令 (exec.Command("where", "go").Output())。
这些具体:
goInstalled, err := exec.Command("where", "go").Output() // does not return output on mac when compiled but does in terminal command. DOES return output on windows compiled.
goInstalled, err := exec.Command("which", "go").Output() // does not return output on mac when compiled but does in terminal command
goInstalled, err := exec.Command("command", "-v", "go").Output() // returns output when compiled and as terminal command (mac only)
如果可能,我想对 windows 和 mac (darwin) 使用相同的命令,而不是创建两个单独的函数来检查用户机器上是否安装了东西。
【问题讨论】:
-
你一定忽略了
err。 -
“无输出”我的意思是它确实触发了 err != nil 块,并且这些 goInstalled 的值为 []。 goInstalled, err := exec.Command("command", "-v", "go").Output() 不会触发 err != nil 块并返回输出 [字节数]
-
exec.Command 不 执行 shell 命令。它执行作为第一个参数给出的程序。如果不显式执行 shell,Shell 内置函数将无法工作。在这种情况下,您可以简单地调用exec.LookPath。