简体   繁体   English

Linux busybox shell 脚本 html 格式化

[英]Linux busybox shell script html formatting

I am having a problem with a shell script and hope you can help.我在使用 shell 脚本时遇到问题,希望您能提供帮助。 I want to optimize the HTML formatting of the following code:我想优化以下代码的 HTML 格式:

#! /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.我删除了echo -e和双引号。 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.注意:上面的代码是 .cfg 文件的内容,每次重启都会加载它。 The .cfg file then pastes the content between the EOF markers into myfile, which is a CGI script. .cfg 文件然后将 EOF 标记之间的内容粘贴到 myfile 中,这是一个 CGI 脚本。

Could that be the problem?这可能是问题吗?

No need for echo -e and quotes,不需要 echo -e 和引号,

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

This works.这有效。

Very close :-) You don't need the echo .非常接近:-) 你不需要echo

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

Add !#/bin/sh to the top, and eliminate the line that begins with echo e, and the line with the single quote before EOF.将!#/bin/sh 添加到顶部,并删除以echo e 开头的行,以及EOF 前带单引号的行。 Then your html will be correctly delivered to myfile.然后你的 html 将被正确地传送到 myfile。

So the correct shell script will be:-所以正确的shell脚本将是:-

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

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. busybox shell 没有在 vi 编辑器中正确显示粘贴的选项卡,所以我使用了 Vim 的set: noai命令。
set: noai solved the tab problem, but there was this extra line after the HTML code. set: noai解决了tab 的问题,但是HTML 代码后面多了一行。
The solution to this was using the \\c escape character.对此的解决方案是使用\\c转义字符。

If someone has a better answer, feel free to post it.如果有人有更好的答案,请随时发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM