0

cat /usr/lib/cgi-bin/test00.cgi

#!/bin/bash
echo "Content-type: text/html"
cat /tmp/file

results is:

one two three four

How format output like a bash script (with newline)?

one
two
three
four
Inian
  • 80,270
  • 14
  • 142
  • 161
Pol Hallen
  • 1,852
  • 6
  • 32
  • 44
  • 2
    Have you tried `text/plain` instead of `text/html`? – Ruud Helderman Sep 07 '17 at 10:46
  • From where is `one two three four` printed? And where is the `echo` string `Content-type: text/html` – Inian Sep 07 '17 at 10:46
  • 1
    @Inian OP is writing a [CGI](https://stackoverflow.com/questions/2089271/what-is-common-gateway-interface-cgi) script. So I assume OP refers to the way _a web browser_ is rendering the resulting HTTP response. `Context-type` is part of the HTTP header and as such not directly visible. The rest is apparently the content of `/tmp/file`. – Ruud Helderman Sep 07 '17 at 11:08

2 Answers2

3

Here you can use both html and UNIX commands

#!/bin/bash

echo Content-type: text/html
echo ""

/bin/cat << EOM
<HTML>
<HEAD><TITLE>File Output: /tmp/file </TITLE>
</HEAD>
<BODY bgcolor="#cccccc" text="#000000">
<HR SIZE=5>
<H1>File Output: /tmp/file </H1>
<HR SIZE=5>
<P>
<SMALL>
<PRE>
EOM

/bin/cat /tmp/file

CAT << EOM
</PRE>
</SMALL>
<P>
</BODY>
</HTML>
EOM
watchmansky
  • 190
  • 11
0

Text in HTML is a subject of https://developer.mozilla.org/en-US/docs/Glossary/Inline-level_content so all white-spaces (including new lines) are replaced with a single (or more depending on alignment) space.

To alter rendering you enclose text into another HTML elements with assigned rendering model:

<div>line 1</div>
<div>line 2</div>

or:

<pre>
line 1
line 2
</pre>

Bash has convenient here-document syntax << to code above:

#!/bin/bash

cat <<EOF
Content-Type: text/html

<pre>
line 1
line 2
</pre>
EOF

Alternatively you could use Content-Type text/plain to preserve original text formatting:

#!/bin/bash

cat <<EOF
Content-Type: text/plain

line 1
line 2
EOF
gavenkoa
  • 45,285
  • 19
  • 251
  • 303