cgi-bin, bash and formatting output

1.6k views Asked by At

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
2

There are 2 answers

0
watchmansky On BEST ANSWER

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
0
gavenkoa On

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