有什么办法可以在golang的一个进程中运行多个命令“ os / exec”?

i want run multiple command in "os/exec" in one process, as you see below some command like "cd" not worked.

func main()  {
cmd := exec.Command("ls")
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
err := cmd.Run()
fmt.Print(string(cmdOutput.Bytes()))

fmt.Println("..........  cd   .........")
cdOutput := &bytes.Buffer{}
cdcomand:=exec.Command("cd","model")
cdcomand.Stdout = cdOutput
err = cdcomand.Run()
fmt.Print(string(cdOutput.Bytes()))
fmt.Println(".......... ls .........")
lsOutput := &bytes.Buffer{}
lscmd:=exec.Command("ls")
lscmd.Stdout = lsOutput

err = lscmd.Run()
if err != nil {
    os.Stderr.WriteString(err.Error())
}
fmt.Print(string(lsOutput.Bytes()))}


i try with another way:
package main

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

func main()  {
cmd := exec.Command("ls")
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
err := cmd.Run()
fmt.Print(string(cmdOutput.Bytes()))

fmt.Println("..........  cd and ls   .........")
cdOutput := &bytes.Buffer{}
cdcomand:= exec.Command("sh", "-c", "ls && cd model")
cdcomand.Stdout = cdOutput
err = cdcomand.Run()
fmt.Print(string(cdOutput.Bytes()))
fmt.Println(".......... ls .........")
lsOutput := &bytes.Buffer{}
lscmd:=exec.Command("ls")
lscmd.Stdout = lsOutput

err = lscmd.Run()
if err != nil {
    os.Stderr.WriteString(err.Error())
}
fmt.Print(string(lsOutput.Bytes()))
}

it did not work too.

in cmd document writes:

A Cmd cannot be reused after calling its Run, Output or CombinedOutput methods.

i've searched all tuts and docs for a way to do this, but i could not find any things. there was no solution in Executing external commands in Go article and advanced command execution in Go with os

each cmd command execute in different process so command like "cd" will not change directory .
is there any way run multiple command "os/exec" in one proccess in golang?

Yes!

You could use sh -c "ls && cd model"

cmd := exec.Command("sh", "-c", "ls && cd model")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()

On ubuntu

$ man sh

DASH(1)                                                              BSD General Commands Manual                                                              DASH(1)

NAME
     dash — command interpreter (shell)

           -c               Read commands from the command_string operand instead of from the standard input.  Special parameter 0 will be set from the command_name
                            operand and the positional parameters ($1, $2, etc.)  set from the remaining argument operands.

An example using:

$ go version
go version go1.10.2 linux/amd64

// cmd/test/main.go

package main

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


func main() {
        var stdout, stderr bytes.Buffer
        cmd := exec.Command("sh", "-c", "echo 'hello' && echo 'hi'")
        cmd.Stdout = &stdout
        cmd.Stderr = &stderr
        err := cmd.Run()
        fmt.Println(err)
        out := stdout.String() + stderr.String()
        fmt.Printf(out)
}

$ go run cmd/test/main.go
<nil>
hello
hi