在OS/exec.cmd.wait()之后保存Stdout
我正在与OS/EXEC合作,在命令运行时发送输入和接收输出。 我需要在命令完成后存储命令的返回代码,因此我有一个带有err:= cmd.wait()
的goroutine,并且我从err中获得任何失败返回代码。 但是,wait()似乎也丢弃了我还需要的剩余stdout。 那么,如何在cmd.wait()之后保留OS/Exec.cmd的剩余stdout?
示例代码,使用UNIX BC计算器命令:
package main
import (
"fmt"
"os/exec"
"bufio"
"io"
"time"
)
func main() {
cmd := exec.Command("sh", "-c", "bc")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
scanner := bufio.NewScanner(stdout)
cmd.Start()
go func() {
cmd.Wait()
fmt.Println("finished")
}()
io.WriteString(stdin, "1 + 2\n")
fmt.Println(scanner.Scan(), scanner.Text())
io.WriteString(stdin, "3 + 4\n")
fmt.Println(scanner.Scan(), scanner.Text())
io.WriteString(stdin, "5 + 6\n")
io.WriteString(stdin, "quit\n") // cmd.Wait() runs
time.Sleep(time.Second)
// Prints false :(
fmt.Println(scanner.Scan(), scanner.Text())
}
此打印: 正确3 正确7 完成的 错误
我想要的 : 正确3 正确7 完成的 没错11
我还尝试将CMD.STDOUT设置为字节。Buffer喜欢:
var buf bytes.Buffer
cmd.Stdout = &buf
scanner := bufio.NewScanner(&buf)
但这是不可靠的。除非我随着时间的延迟添加。
I'm working with os/exec, sending input and receiving output as a command runs.
I need to store the command's return code when it finishes, so I have a goroutine with err := cmd.Wait()
, and I get any failure return code from the err.
But Wait() seems to throw away the remaining stdout which I need also.
So how do I preserve the remaining stdout of a os/exec.Cmd after Cmd.Wait()?
Example code, using the Unix bc calculator command:
package main
import (
"fmt"
"os/exec"
"bufio"
"io"
"time"
)
func main() {
cmd := exec.Command("sh", "-c", "bc")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
scanner := bufio.NewScanner(stdout)
cmd.Start()
go func() {
cmd.Wait()
fmt.Println("finished")
}()
io.WriteString(stdin, "1 + 2\n")
fmt.Println(scanner.Scan(), scanner.Text())
io.WriteString(stdin, "3 + 4\n")
fmt.Println(scanner.Scan(), scanner.Text())
io.WriteString(stdin, "5 + 6\n")
io.WriteString(stdin, "quit\n") // cmd.Wait() runs
time.Sleep(time.Second)
// Prints false :(
fmt.Println(scanner.Scan(), scanner.Text())
}
This prints:
true 3
true 7
finished
false
I'd like:
true 3
true 7
finished
true 11
I also tried setting cmd.Stdout to a bytes.Buffer like:
var buf bytes.Buffer
cmd.Stdout = &buf
scanner := bufio.NewScanner(&buf)
But that was unreliable. It printed all false unless I added in delays with time.Sleep().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读到Stdout结束后,请致电CMD.Wait()。
选项1:在scanner.scan()返回false之后,从主goroutine调用CMD.Wait。
选项2:从等待Goroutine阅读:
Call cmd.Wait() after reading to the end of stdout.
Option 1: call cmd.Wait from the main goroutine after scanner.Scan() returns false.
Option 2: read from the waiting goroutine: