I'm trying to stream the Stdout of a shell command to the console, but am having difficulty.
Here's what I currently have:
cmd := exec.Command("sh", "-c", `for number in {0..10}; do echo "$number "; done;`)
pipe, _ := cmd.StdoutPipe()
reader := bufio.NewReader(pipe)
line, err := reader.ReadString('\n')
for err == nil {
fmt.Println(line)
line, err = reader.ReadString('\n')
}
I would expect this to print out the numbers 0 through 10, but it seems to hang on line 3 (the first call to ReadString
.
I started with cmd.Output()
and cmd.CombinedOutput()
, but those methods seem to buffer the entire output stream until the command is complete. I need to process the output as it streams, not wait until the command is complete.
I also tried this: continuously reading from exec.Cmd output, but it didn't seem to work and I went away from it because I really want to read lines and not have to manage the buffer manually.
Other things I've looked through:
You need to start the command:
cmd := exec.Command("sh", "-c", `for number in {0..10}; do echo "$number "; done;`)
pipe, _ := cmd.StdoutPipe()
if err := cmd.Start(); err != nil {
// handle error
}
reader := bufio.NewReader(pipe)
line, err := reader.ReadString('\n')
for err == nil {
fmt.Println(line)
line, err = reader.ReadString('\n')
}
Call Wait after reaching EOF.
The Output and CombinedOutput methods worked for you because these methods call Start internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With