PHP
·
发表于 5年以前
·
阅读量:8279
package main
import (
"fmt"
"io/ioutil"
"os/exec"
)
func main(){
cmd := exec.Command("/bin/bash", "-c", "grep test")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
if err := cmd.Start(); err != nil{
fmt.Println("Execute failed when Start:" + err.Error())
return
}
stdin.Write([]byte("go text for grep\n"))
stdin.Write([]byte("go test text for grep\n"))
stdin.Close()
out_bytes, _ := ioutil.ReadAll(stdout)
stdout.Close()
if err := cmd.Wait(); err != nil {
fmt.Println("Execute failed when Wait:" + err.Error())
return
}
fmt.Println("Execute finished:" + string(out_bytes))
}