如何在读取stdout时保留shell脚本的行间距



我正在Windows 10上通过Cygwin执行一个shell脚本,将stdout和stderr读取到一个字符串中,并将该字符串传递到一个用于执行html模板的结构中。

问题是,在执行模板之后,输出的间距没有得到保留。以下是我如何执行命令并将命令输出设置为我的数据结构。

var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err = cmd.Run()
if err != nil {
log.Printf("Error running command: %s", err)
data.Output = errbuf.String()
return data
}
log.Printf("%v", outbuf.String())
data.Output = outbuf.String()
return data

日志。Printf会产生这样的东西,这正是我所期望的。将显示新行。

If the same archived message file is in both system/logs and archived_logs, it will be searched/counted twice!
====X1-SC1====

Done

在运行命令并更新我的数据结构之后,将使用html模板执行数据。

err = t.Execute(w, data)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Failed to execute html: %v", err)))
return
}
}
<div style="text-align:center">
{{.Output}}
</div>

浏览到我的项目公开的位置后,我看到了我期望的输出,但这都在一行上。间距未保留。似乎html/template执行正在删除多余的空白、行或类似的内容。有办法绕过这个吗?

If the same archived message file is in both system/logs and archived_logs, it will be searched/counted twice! Maybe it'll be fixed in the future.. ====X1-SC1==== Done

查看源代码,会发现模板保留了空白。问题是浏览器正在折叠空白序列。请参阅developer.mozilla.org/en-US/docs/Web/CSS/white-space。

最新更新