func main() {
    msg, err := ExecAsRes("ls -al|grep test")
    fmt.Println(msg, err)
}

// ExecAsRes 执行shell指令,有返回值
func ExecAsRes(cmd string) (msg string, err error) {
    param := append([]string{"-c"}, cmd)
    command := exec.Command("bash", param...)
    out, err := command.CombinedOutput()
    if err != nil {
        fmt.Println(err)
        return "", err
    }
    return string(out), err
}

// ExecNoRes 执行shell指令,无返回值
func ExecNoRes(command string) error {
    in := bytes.NewBuffer(nil)
    cmd := exec.Command("sh")
    cmd.Stdin = in
    in.WriteString(command)
    if err := cmd.Run(); err != nil {
        return err
    }
    return nil
}