给定一个任意字符串,我如何将命令打包为bash?

给定一个任意字符串,我如何将命令打包为bash?

问题描述:

I have a pretty basic golang question.

Given an arbitrary string like "echo foo", or "CGO_ENABLED=0 go build -o ./bin/echo -a main.go", what is the idiomatic way to parse/run that command using os/exec?

The way that I got this working seems pretty hacky, and I'm looking for a more idiomatic approach.

userInput = "CGO_ENABLED=0 go build -o ./bin/echo -a main.go"
command := exec.Command("/bin/bash", "-c", userInput)
out, err := command.Output()
if err != nil {
    fmt.Println(err)
 }

我有一个非常基本的golang问题。 p>

给出一个任意字符串,例如 “ echo foo” code>或“ CGO_ENABLED = 0 go build -o ./bin/echo -a main.go” code>,解析/运行该代码的惯用方式是什么 使用 os / exec code>的命令? p>

我的工作方式似乎很hacky,我正在寻找一种更惯用的方法。 p>

  userInput =“ CGO_ENABLED = 0 go build -o ./bin/echo -a main.go” 
command:= exec.Command(“ / bin / bash”,“ -c”  ,userInput)
out,err:= command.Output()
if err!= nil {
 fmt.Println(err)
} 
  code>  pre> 
  div>

Depending on how arbitrary and flexible your userInput string is, I might do something like this.

package main

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

func main() {
    if err := os.Setenv("FOO", "bar"); err != nil {
        log.Println(err)
    }

    // As Dave C pointed out, exec.Command does this for you.
    // goAbsPath, err := exec.LookPath("go")
    // if err != nil {
    //   log.Println(err)
    // }

    // Pass whatever other arguments necessary.
    cmd := exec.Command("go", "version")
    if err = cmd.Run(); err != nil {
        log.Println(err)
    }
}

If nothing else, maybe os.Setenv and exec.LookPath might be useful to know about.

I may write like this:

cmd := exec.Command(cmdName, arg1, arg2, ..)   
cmd.Env = append(cmd.Env, "foo=bar")

err = cmd.Start()
if err != nil {
    // log or something else
}

err = cmd.Wait()
if err != nil {
    if exitErr, ok := err.(*exec.ExitError); ok {
        if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
            // handle err according to different status.ExitStatus()
        }
        // ...
    }
    // ...
}
// ...

Godoc explains quite well if you need more details.