douqi3195 2019-01-09 22:59
浏览 1148

为什么有些命令为exec.Command()返回输出,而其他命令则不返回

I am trying to figure out why some shell commands work with goloang exec.Command and others don't when they all return the same result when entered in terminal. I basically would like to use the same command for windows and mac binary (exec.Command("where", "go").Output()).

These specifically:

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)

I would like to use the same command for windows and mac (darwin) if possible rather than create two separate functions to check if things are installed on users machine.

  • 写回答

2条回答 默认 最新

  • dongtang2376 2019-01-10 06:59
    关注

    The main issue you're running into here is that exec.Command executes programs, where the commands you're trying to use are actually built-ins.

    In order to use built-ins, you need to find them using exec.LookPath, and they're not generally available outside of a shell.

    If you must, you can also execute the command from within a shell, by having a shell be first argument:

    exec.Command("/bin/bash", "-c", "command -v foo")
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决