Bash-Shell script show source code, not html - bash

It's weird that my shell script in website show the whole source code, not the html that I want.
My script is:
#!/bin/sh
echo "Content-type: text/html"
echo ""
cat << EOF
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<title>test</title>
<link type="text/css" href="../jquery/css/redmond/jquery-ui-custom.min.css" rel="stylesheet">
<script type="text/javascript" src="../jquery/js/jquery.min.js"></script>
<script type="text/javascript" src="../jquery/js/jquery-ui-custom.min.js"></script>
<script type="text/javascript">
//<![CDATA[
if(verifiedUser == "")
window.top.location.href = "/";
//]]>
</script>
</head>
<body>
<table align="center">
<tr><td align="right">Product Name</td><td align="left">
</td></tr>
<tr><td align="right">Hardware Version</td><td align="left">
</td></tr>
<tr><td align="right">Firmware Version</td><td align="left">
</td></tr>
</table>
</body>
</html>
EOF
and what the .cgi file show is just that code!
My cgi file is in the same directory of the html files, because the main directory is in /mnt, so not put in /home/www/cgi-bin/ as usual.
What's the reasonable problem can cause this situation?
Any advice appreciated!

AFAIK, the separator between header section & the html data should be a blank line in CRLF format.
Try this:
echo -e "Content-type: text/html\r"
echo -e "\r"
cat << 'EOF'
---remaining HTML data---
EOF
Note: I have also changed cat << EOF to cat << 'EOF' to prevent any variable expansion. Revert it if variable/command expansion is needed.

#Barmar has the right idea - Your server is very likely not configured to run those files, but rather send the file directly to the client. So it's a configuration issue, not a programming one.

Related

How to index file contents

I have a server at batalabs.tk where I use batch files as most of my CGI scripts, I am currently trying to make an Indexing service for a search script...
Example files:
Directory to search:
<DIR HTDOCS>
index.html
otherpage1.html
otherpage2.html
</DIR>
index.html:
<HTML>
<HEAD>
<TITLE>MyWebsite - Buy Products</TITLE>
<META NAME="Description" Content="Buy Software and products from MyWebsite">
</HEAD>
<BODY>
<DIV ALIGN="CENTER">
<H2>Software</H2>
<h4>Find the latest and greatest in software right here at the Shop Page!</h4>
<hr>
Lorum - Free Cloud Storage
<hr>
Ipsum - Free Networking Software
<hr>
</div>
Copyright MyWebsite.com, 2016
</body>
</html>
Otherpage1.html
<html>
<head>
<title>Page One - Hello World!</title>
<META Name="Description" Content="Hello World Page For Testing.">
</head>
<body>
<h1>
Hello, World!
</h1>
<div>
<pre>
this is an example page for use with testing.
Any of these words can be searched and should (optimally) show this page in the results.
</pre>
</div>
</body>
</html>
otherpage2.html:
<html>
<head>
<title>Page Two - The Revenge Of The Coder</title>
<meta Name="Tags" content="dummy; coder; file; otherpage2;;">
</head>
<body>
<pre>
Hello, This file doesn't matter. At all. It is a dummy.
</pre>
</body>
</html>
These are just example files, But i want my batch file to pick up on the
<META> tag with Description and Tags, as well as the file's name, every word in the body,
and the title... I've tried some stuff to no avail, any help?
So maybe a user could type "dummy" as their keyword, and Otherpage2.html would pop up like so:
Page Two - The Revenge Of The Coder
HTTPS://www.batalabs.tk/otherpage2.html
Hello, this file doesn't matter. At all. It is a dummy.

Bash Script to generate HTML Template

I want a Bash script that generates a HTML template. The template will include libraries based on the input arguments at the command line. Here is my idea:
#!/bin/bash
function libraries
{
if [ "$1" == "bootstrap" ]; then
echo "<link type="text/css" rel="stylesheet" href="css/bootstrap.css" />"
fi
}
##### Main
cat << _EOF_
<!DOCTYPE html>
<html>
<head>
<title> </title>
$(libraries)
</head>
<body>
</body>
</html>
_EOF_
When I run ./template_creator.sh bootstrap I get this output:
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
</body>
</html>
If I don't include the if statement and just echo, it outputs fine. So I am thinking the trouble is in the if statement. Any suggestions?
You can use printf to inject variables in your template.
In your template use the printf formats (eg. %s) as inserting points.
This way you can even use a file for your template (calling it with tpl=$(cat "main.tpl") and handle your variables in your script.
libraries() {
if [ "$1" == "bootstrap" ]; then
link='<link type="text/css" rel="stylesheet" href="css/bootstrap.css" />'
printf "$tpl" "$link"
fi
}
##### Main
read -d '' tpl << _EOF_
<!DOCTYPE html>
<html>
<head>
<title> </title>
%s
</head>
<body>
</body>
</html>
_EOF_
libraries "$1"
Inside a function, $1 refers to the first argument passed to that function, not the first argument passed to the script. You can either pass the script's first argument on to the function by using $(libraries "$1") in the here-document, or assign it to a global variable at the beginning of the script and then use that in the function.
Instead of $(libraries) write $(libraries) $1.

Render sqlite output into template file, using bash script

I have template-like file, with some simple HTML in it.
<html>
<head>
<meta charset="utf-8">
<title>stackoverflow.com</title>
<link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8">
</head>
<body id="container">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<th>Field 1 Label</th>
<th>Field 2 Label</th>
<th>Field 3 Label</th>
</tr>
<!-- ...dynamically generated content from other file should go here... -->
<!-- ...instead of this template tag... -->
${sqlite_html_output} <!-- this is a placeholder I want to use -->
</table>
</body>
</html>
The file I want to render into this template, will be the output of sqlite query contained in the following function:
function generate_report() {
sqlite3 -batch database.db <<- "end_of_message"
.mode html
.output o.html # <-- this is the name of generated output file
select field1,field2,field3 from tbl;
end_of_message
#..here I want to use `sed` to embed content of o.html file, into my template
}
So in my generate_report function, I want to use sed to render that o.html
file, into my template, in place of that ${sqlite_html_output} template-tag placeholder.
Would that be possible?
Try this:
sed '/\${sqlite_html_output}/{r o.html
d;}' template_file
It should search for a line matching ${sqlite_html_output} and, when matching it, insert text read from o.html then delete content of line (i.e. ${sqlite_html_output} <!-- this is a placeholder I want to use --> in your example).
Line break is mandatory.

Can I put HTML into a variable?

Using the Sinatra library, I'm trying to condense two functions that display HTML code into a single function. Both these functions differ by only a small amount of HTML.
Here's an example.
def make_start_page()
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> Hello </p>
<img src="..." />
</body>
</html>
EOS
end
def make_guess_page()
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> Something different </p>
<a href="..." >1</a>
</body>
</html>
EOS
end
In the Ruby function that will call these two functions, I was wondering if it is possible to take the small portion of HTML that differs and pass it to a single, condensed version of these two functions that will display the page.
def handle()
if 1
var = "<p> Hello </p>
<img src="..." />"
elsif 2
var = "<p> Something different </p>
<a href="..." >1</a>"
make_start_guess_page(var)
end
You can interpolate variables in heredoc:
def make_start_page(var)
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
#{var}
</body>
</html>
EOS
end
For example.
There no reason why you could not do that. However if you want to print it, you'll probably have to use functions like String#html_safe in rails, or != in haml

Using ruby gem coderay gets me inline text in *.html.erb file

I am focused on the wrong layer of abstraction here, but can't figure out where.
I have this file views/pages/overview.html.erb
<%= stylesheet_link_tag "cust/coderay"%>
<h1>Overview</h1>
<hr>
Here's my code test:
<%= html = CodeRay.scan("puts 'Hello, world!'", :ruby).div(:line_numbers => :table)%>
<hr>
Back <%=link_to "home", "home"%>.
<hr>
It took <%="%.3f" %(Time.now-#start_time)%> seconds to generate this page.
To my surprise, the pages renders like so:
When I view source on the page I get:
<!DOCTYPE html>
<html>
<head>
<title>Dash</title>
<link href="/assets/application-all.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/all/pages.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/pages.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="SydEiDhSNHuEE6vCfr4rajIksxBbqnm89sddC08msjs=" name="csrf-token" />
</head>
<body>
<h1>Overview</h1>
<hr>
Here's my code test:
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>
</pre></td>
<td class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello, world!</span><span style="color:#710">'</span></span></pre></td>
</tr></table>
<hr>
Back home.
<hr>
It took 0.006 seconds to generate this page.
</body>
</html>
Why is the bracketed css displaying as inline text? What should my usage of coderay look like here?
Many thanks -
Rails escapes your HTML by default in ERB templates. You need to turn off HTML escaping like so:
<%=raw CodeRay.scan("puts 'Hello, world!'", :ruby).div(:line_numbers => :table) %>
See more at this question and these release notes.

Resources