Exec a shell command in Go

前端 未结 7 1615
甜味超标
甜味超标 2020-12-04 10:01

I\'m looking to execute a shell command in Go and get the resulting output as a string in my program. I saw the Rosetta Code version:

package main
import \"f         


        
7条回答
  •  清歌不尽
    2020-12-04 10:37

    // 封装exec ,有shell= true 这样的选项
    
    func Cmd(cmd string, shell bool) []byte {
    
    if shell {
        out, err := exec.Command("bash", "-c", cmd).Output()
        if err != nil {
            panic("some error found")
        }
        return out
    } else {
        out, err := exec.Command(cmd).Output()
        if err != nil {
            panic("some error found")
        }
        return out
    
    }
    }
    

    you may try this .

提交回复
热议问题