为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
侃侃无极 2023-07-31 16:49:12
我正在 Windows 10 上通过 Cygwin 执行 shell 脚本,将 stdout 和 stderr 读取为字符串,并将该字符串传递给用于执行 html 模板的结构。问题是执行模板后不会保留输出的间距。以下是我执行命令并将命令输出设置到我的数据结构的方式。var outbuf, errbuf bytes.Buffercmd.Stdout = &outbufcmd.Stderr = &errbuferr = 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 datalog.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/模板执行似乎正在删除额外的空白、行或类似的东西。有没有解决的办法?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
查看完整描述

1 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

查看源码,会发现模板保留了空格。问题在于浏览器正在折叠空白序列。


查看完整回答
反对 回复 2023-07-31
  • 1 回答
  • 0 关注
  • 57 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信