如何在读取 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

log.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


侃侃无极
浏览 60回答 1
1回答

翻翻过去那场雪

查看源码,会发现模板保留了空格。问题在于浏览器正在折叠空白序列。
打开App,查看更多内容
随时随地看视频慕课网APP