簡體   English   中英

如何執行shell命令

[英]How to execute shell command

我在從 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 程序引發異常,如file /home/slavik/project/build not found

我的代碼有什么問題?

您必須分別傳遞程序和參數。 查看exec.Command的簽名:

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

因此,如果您想通過 eg -v=1 ,您的電話可能看起來像:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM