如图执行, 加上find执行的失败,如果把管道后面find去除,可以成功执行。也就是这种方式
调用系统命令是正确的,是不是windows用go执行find会有权限问题呢?![8DCB.tmp.png](http://studygolang.qiniudn.com/160902/c42556e3525091a2d0c14a3aadf481b6.png)![8DCB.tmp.png](http://studygolang.qiniudn.com/160902/c42556e3525091a2d0c14a3aadf481b6.png)
可能只能分行提取吧,没有发现更好的方法。
```
package main
import (
"bytes"
"fmt"
"io"
"os/exec"
"strings"
)
func main() {
cmd := exec.Command("cmd.exe")
out := &bytes.Buffer{}
w, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
cmd.Stdout = out
if err := cmd.Start(); err != nil {
panic(err)
}
io.WriteString(w, `echo "helloworld" | find /i "hello"`)
io.WriteString(w, "\n")
w.Close()
if err := cmd.Wait(); err != nil {
panic(err)
}
lines := strings.Split(out.String(), "\n")
fmt.Println(lines[4])
}
```
#7
更多评论
在windows下应该是这样:
```
package main
import (
"bytes"
"fmt"
"io"
"os/exec"
)
func main() {
cmd := exec.Command("cmd.exe")
out := &bytes.Buffer{}
cmd.Stdout = out
w, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
if err := cmd.Start(); err != nil {
panic(err)
}
io.WriteString(w, `echo "helloworld" | find /i "hello"`)
io.WriteString(w, "\n")
w.Close()
if err := cmd.Wait(); err != nil {
panic(err)
}
fmt.Println(out.String())
}
```
#2