Loading

Create a HTML Document using shell script in Linux

You can actually create a HTML document using the shell script. If you are tired to see the output of your command in the terminal, you can redirect your output to the HTML document. This will allow you to see the output in the web browser like the website. I will assume, you know the basics of HTML. Even if you don’t, it’s very easy to learn. Find some good tutorials and get started.Now, let’s get started by writing a simple shell script with HTML tags in it.

 #!/bin/sh
echo "<htmL>"
echo "<head>"
echo "<title>"
echo "Output in a HTML Document"
echo "</title>"
echo "</head>"
echo "<body>"
cat freshtutorial
echo "</body>"
echo "</html>"


This simple shell script called “test” will display the file called freshtutorial which I have already created in my home directory. Every HTML tags have to be quoted with echo. Redirect the script to HTML document.

sh test >> test.html

Open the test.html and see the output. Instead of creating a output into the terminal itself, it redirects the output to a webpage.

We don’t want to put echo in every HTML tags. This will make our job tedious and dirty. Thus to simplify this we will use “<< “.


#!/bin/bash
display="HELLO WORLD"
cat << noEcho
<HTML>
<HEAD>
<TITLE> Cool Bash trick </TITLE>
</HEAD>
<BODY>
$display
</BODY>
</HTML>

noEcho


“noEcho” is called token in the above script named “display”. You can assign any token as you want besides the bash keywords. Redirect the script into a webpage issuing the following command:

sh display >> display.html

This all might seem confusion at the beginning. Once you get used to it, it feels amazing. It is a nice cool shell programming trick as well. If you have any confusion, feel free to ask questions as comment.

0 comments:

Post a Comment

Facebook Badge

 
Design by Cybermoshfiq | Bloggerized by Moshfiqur's Rahman - .