Question

I am having a problem with a shell script and hope you can help. I want to optimize the HTML formatting of the following code:

#! /bin/sh

cat <<EOF > myfile # temporary file
#! /bin/sh

echo -e "Content-type: text/html; charset=iso-8859-1\n\n"
echo -e "<html><head>\c"
echo -e "<title></title>"
echo -e "</head>"
echo -e "<body>\c"
echo -e "<p>Text</p>"
echo -e "</body></html>"

EOF
chmod 777 myfile
mount -o bind myfile myfile # mount temporary on the original myfile
rm myfile

I deleted the echo -e and double quotes. I also tried this:

#! /bin/sh

cat <<EOF > myfile # temporary file
#! /bin/sh

echo -e '
<html>
    <head>
        <title></title>
    </head>
        <body>
            <p>Text</p>
        </body>
</html>
'

EOF
chmod 777 myfile
mount -o bind myfile myfile # mount temporary on the original myfile
rm myfile

What is wrong with the script?

Note: The code above is content of a .cfg file, which gets loaded with every reboot. The .cfg file then pastes the content between the EOF markers into myfile, which is a CGI script.

Could that be the problem?

Was it helpful?

Solution 4

I finally found the solution:

#! /bin/sh

cat <<EOF > myfile # temporary file
#! /bin/sh

echo -e "Content-type: text/html; charset=iso-8859-1"
echo -e "
<html>
    <head>
        <title></title>
    </head>
        <body>
            <p>Text</p>
        </body>
</html>\c"

EOF

chmod 777 myfile
mount -o bind myfile myfile # mount temporary on the original myfile
rm myfile

The busybox shell didn't display the pasted tabs in the vi editor correctly, so I used Vim's set: noai command.
set: noai solved the tab problem, but there was this extra line after the HTML code.
The solution to this was using the \c escape character.

If someone has a better answer, feel free to post it.

OTHER TIPS

No need for echo -e and quotes,

cat << eof > shellhtml.htm
<html>
</html>
eof

This works.

Add !#/bin/sh to the top, and eliminate the line that begins with echo e, and the line with the single quote before EOF. Then your html will be correctly delivered to myfile.

So the correct shell script will be:-

#!/bin/sh
cat <<EOF > myfile
 <html>
 <head>
 <title></title>
 </head>
     <body>
     <p>Text</p>
     </body>
 </html>
EOF

Very close :-) You don't need the echo.

#! /bin/sh 
cat <<EOF > myfile
<html>
    <head>
    <title></title>
    </head>
        <body>
        <p>Text</p>
        </body>
</html>
EOF
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top