Go 代码:
func A() {
c := exec.Command("bash")
var i = bytes.NewBuffer(nil)
var o = bytes.NewBuffer(nil)
c.Stdin = i
c.Stdout = o
c.Stderr = o
if err := c.Start(); err != nil {
panic(err.Error())
}
i.Write([]byte("sudo echo abc"))
i.Write([]byte("1")) // root password
if err := c.Wait(); err != nil {
panic(err.Error())
}
fmt.Println("标准 /错误输出:", o.String())
}
