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?