为什么阅读 Stdout.Pipe 会重复文本?
Why does reading from Stdout.Pipe repeats text?
我正在尝试从命令的标准输出中解析一些错误。作为命令,我使用以下示例脚本:
#!/bin/bash
for i in {1..4}
do
echo "[the script] working... working..."
sleep 0.5s
echo "[error 1010] This is an error that occured just in this moment."
sleep 0.5s
done
exit 41
我的解析代码如下所示(导入缩短):
func main() {
cmd := exec.Command("./test.sh")
os.Exit(stdOutPipe(cmd))
}
func stdOutPipe(cmd *exec.Cmd) int {
stdout, _ := cmd.StdoutPipe()
cmd.Start()
chunk := make([]byte, 20)
for {
_, err := stdout.Read(chunk)
if err != nil {
break
}
s := string(chunk)
if strings.Contains(s, "error 1010") {
fmt.Println("[your genius go tool] There occurred an ERROR and it's number ist 1010!")
break
}
fmt.Print(s)
}
cmd.Wait()
return cmd.ProcessState.ExitCode()
}
我得到以下输出:
$ go run main.go
[the script] working... working...
rking[your genius go tool] There occurred an ERROR and it's number ist 1010!
exit status 41
输出的第二行重复上一行的“rking”。我该如何摆脱这个?如果您能解释为什么会出现这种重复,那就太好了。
您正在丢弃 read
的 return 值。这会告诉您读取了多少字节。
n, err := stdout.Read(chunk)
s:=string(chunk[:n])
if strings.Contains(s, "error 1010") {
fmt.Println("[your genius go tool] There occurred an ERROR and it's number ist 1010!")
break
}
fmt.Print(s)
if err != nil {
break
}
根据阅读文档:
If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more
我正在尝试从命令的标准输出中解析一些错误。作为命令,我使用以下示例脚本:
#!/bin/bash
for i in {1..4}
do
echo "[the script] working... working..."
sleep 0.5s
echo "[error 1010] This is an error that occured just in this moment."
sleep 0.5s
done
exit 41
我的解析代码如下所示(导入缩短):
func main() {
cmd := exec.Command("./test.sh")
os.Exit(stdOutPipe(cmd))
}
func stdOutPipe(cmd *exec.Cmd) int {
stdout, _ := cmd.StdoutPipe()
cmd.Start()
chunk := make([]byte, 20)
for {
_, err := stdout.Read(chunk)
if err != nil {
break
}
s := string(chunk)
if strings.Contains(s, "error 1010") {
fmt.Println("[your genius go tool] There occurred an ERROR and it's number ist 1010!")
break
}
fmt.Print(s)
}
cmd.Wait()
return cmd.ProcessState.ExitCode()
}
我得到以下输出:
$ go run main.go
[the script] working... working...
rking[your genius go tool] There occurred an ERROR and it's number ist 1010!
exit status 41
输出的第二行重复上一行的“rking”。我该如何摆脱这个?如果您能解释为什么会出现这种重复,那就太好了。
您正在丢弃 read
的 return 值。这会告诉您读取了多少字节。
n, err := stdout.Read(chunk)
s:=string(chunk[:n])
if strings.Contains(s, "error 1010") {
fmt.Println("[your genius go tool] There occurred an ERROR and it's number ist 1010!")
break
}
fmt.Print(s)
if err != nil {
break
}
根据阅读文档:
If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more