【问题标题】:How to execute shell command如何执行shell命令
【发布时间】:2013-06-09 11:18:44
【问题描述】:

我在从 Go 程序执行 shell 命令时遇到了一些问题:

  var command = pwd + "/build " + file_name

  dateCmd := exec.Command(string(command))
  dateOut, err := dateCmd.Output()
  check(err)

如果command 变量等于像/home/slavik/project/build 这样的单个单词(构建是shell 脚本),它可以工作,但是如果我尝试传递一些参数,即/home/slavik/project/build xxx/home/slavik/project/build -v=1,Go 程序会引发像@987654327 这样的异常@

我的代码有什么问题?

【问题讨论】:

标签: bash go exec


【解决方案1】:

您必须分别传递程序和参数。见exec.Command的签名:

func Command(name string, arg ...string) *Cmd

所以如果你想通过例如-v=1,你的电话应该是这样的:

dateCmd := exec.Command(pwd + "/build", "-v=1")

【讨论】:

    【解决方案2】:

    使用

    exec.Command(pwd + "/build", fileName)
    

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2013-01-05
      相关资源
      最近更新 更多