exec.Command()
最简单的方法:
cmd := exec.Command("/bin/sh/", "-c", "linux命令")
或复杂的, 各个参数都要单独写
cmd := exec.Command("ls", "-l", "-a")
package main import ( "bytes" "fmt" "os/exec" ) func main() { in := bytes.NewBuffer(nil) cmd := exec.Command("sh") cmd.Stdin = in go func() { in.WriteString("echo hello world > test.txt\n") in.WriteString("exit\n") }() if err := cmd.Run(); err != nil { fmt.Println(err) return } }