In golang how can I write the stdout of an exec.Cmd to a file?

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I am trying to run a shell command, capture stdout and write that output to a file. However, I seem to be missing a few steps, as the file I am trying to write is empty when the program exists. How can I capture the stdout of the command and write that to a file?

package main  import (     "bufio"     "io"     "os"     "os/exec" )  func main() {      cmd := exec.Command("echo", "'WHAT THE HECK IS UP'")      // open the out file for writing     outfile, err := os.Create("./out.txt")     if err != nil {         panic(err)     }     defer outfile.Close()      stdoutPipe, err := cmd.StdoutPipe()     if err != nil {         panic(err)     }      writer := bufio.NewWriter(outfile)      err = cmd.Start()     if err != nil {         panic(err)     }      go io.Copy(writer, stdoutPipe)     cmd.Wait() } 

回答1:

You need to flush the writer. Add the following:

    writer := bufio.NewWriter(outfile)     defer writer.Flush() 


回答2:

Thanks to KirkMcDonald on the #go-nuts irc channel, I solved this by assigning the output file to cmd.Stdout, which means that stdout writes directly to the file. It looks like this:

package main  import (     "os"     "os/exec" )  func main() {      cmd := exec.Command("echo", "'WHAT THE HECK IS UP'")      // open the out file for writing     outfile, err := os.Create("./out.txt")     if err != nil {         panic(err)     }     defer outfile.Close()     cmd.Stdout = outfile      err = cmd.Start(); if err != nil {         panic(err)     }     cmd.Wait() } 


回答3:

You can also use:

cmd.Stdout = os.Stdout 

which will redirect all cmd output to the OS' standard output.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!