Go: 执行 bash 脚本

huangapple go评论91阅读模式
英文:

Go: execute bash script

问题

如何从我的Go程序中执行一个bash脚本?以下是我的代码:

目录结构:

/hello/
  public/
    js/
      hello.js
  templates
    hello.html
  
  hello.go
  hello.sh

hello.go

cmd, err := exec.Command("/bin/sh", "hello.sh")
  if err != nil {
    fmt.Println(err)
}

当我运行hello.go并调用相关路由时,我在控制台上得到以下输出:

exit status 127 output is

我期望的输出是["a", "b", "c"]

我知道在SO上有一个类似的问题:https://stackoverflow.com/questions/25834277/executing-a-bash-script-from-golang,但我不确定路径是否正确。希望能得到帮助!

英文:

How do I execute a bash script from my Go program? Here's my code:

Dir Structure:

/hello/
  public/
    js/
      hello.js
  templates
    hello.html
  
  hello.go
  hello.sh

hello.go

cmd, err := exec.Command("/bin/sh", "hello.sh")
  if err != nil {
    fmt.Println(err)
}

When I run hello.go and call the relevant route, I get this on my console:

exit status 127
output is

I'm expecting ["a", "b", "c"]

I am aware there is a similar question on SO: https://stackoverflow.com/questions/25834277/executing-a-bash-script-from-golang, however, I'm not sure if I'm getting the path correct. Will appreciate help!

答案1

得分: 5

exec.Command() 返回一个结构体,可以用于其他命令,比如 Run

如果你只想获取命令的输出,请尝试以下代码:

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    out, err := exec.Command("date").Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("The date is %s\n", out)
}

这段代码可以执行 date 命令,并将输出打印出来。

英文:

exec.Command() returns a struct that can be used for other commands like Run

If you're only looking for the output of the command try this:

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    out, err := exec.Command("date").Output()
    if err != nil {
	    log.Fatal(err)
    }
    fmt.Printf("The date is %s\n", out)
}

答案2

得分: 2

你可以使用CombinedOutput()代替Output()。它会输出执行命令的标准错误结果,而不仅仅返回错误代码。
参考:
https://stackoverflow.com/questions/18159704/how-to-debug-exit-status-1-error-when-running-exec-command-in-golang

英文:

You can also use CombinedOutput() instead of Output(). It will dump standard error result of executed command instead of just returning error code.
See:
https://stackoverflow.com/questions/18159704/how-to-debug-exit-status-1-error-when-running-exec-command-in-golang

答案3

得分: 0

请参考 http://golang.org/pkg/os/exec/#Command 中的示例。

你可以通过使用一个输出缓冲区并将其分配给你创建的 cmd 的 Stdout 来尝试,如下所示:

var out bytes.Buffer
cmd.Stdout = &out

然后可以使用以下方式运行命令:

cmd.Run()

如果执行成功(即返回 nil),命令的输出将在 out 缓冲区中,可以使用以下代码获取其字符串形式:

out.String()
英文:

Check the example at http://golang.org/pkg/os/exec/#Command

You can try by using an output buffer and assigning it to the Stdout of the cmd you create, as follows:

var out bytes.Buffer
cmd.Stdout = &out

You can then run the command using

cmd.Run() 

If this executes fine (meaning it returns nil), the output of the command will be in the out buffer, the string version of which can be obtained with

out.String()

huangapple
  • 本文由 发表于 2015年1月4日 19:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/27765143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定