< Previous | Contents | Next >
First Stage: Minimal Document
The first thing we need to know is the format of a well-formed HTML document. It looks like this:
<HTML>
<HEAD>
<TITLE>Page Title</TITLE>
</HEAD>
<BODY>
Page body.
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Page Title</TITLE>
</HEAD>
<BODY>
Page body.
</BODY>
</HTML>
If we enter this into our text editor and save the file as foo.html, we can use the fol- lowing URL in Firefox to view the file:
file:///home/username/foo.html
The first stage of our program will be able to output this HTML file to standard output. We can write a program to do this pretty easily. Let’s start our text editor and create a new file named ~/bin/sys_info_page:
[me@linuxbox ~]$ vim ~/bin/sys_info_page
[me@linuxbox ~]$ vim ~/bin/sys_info_page
and enter the following program:
#!/bin/bash
# Program to output a system information page echo "<HTML>"
echo " <HEAD>"
echo " <TITLE>Page Title</TITLE>" echo " </HEAD>"
echo " <BODY>"
echo " Page body." echo " </BODY>"
echo "</HTML>"
#!/bin/bash
# Program to output a system information page echo "<HTML>"
echo " <HEAD>"
echo " <TITLE>Page Title</TITLE>" echo " </HEAD>"
echo " <BODY>"
echo " Page body." echo " </BODY>"
echo "</HTML>"
Our first attempt at this problem contains a shebang, a comment (always a good idea) and a sequence of echo commands, one for each line of output. After saving the file, we’ll make it executable and attempt to run it:
[me@linuxbox ~]$ chmod 755 ~/bin/sys_info_page
[me@linuxbox ~]$ sys_info_page
[me@linuxbox ~]$ chmod 755 ~/bin/sys_info_page
[me@linuxbox ~]$ sys_info_page
When the program runs, we should see the text of the HTML document displayed on the screen, since the echo commands in the script send their output to standard output. We’ll run the program again and redirect the output of the program to the file sys_info_page.html, so that we can view the result with a web browser:
[me@linuxbox ~]$ sys_info_page > sys_info_page.html
[me@linuxbox ~]$ firefox sys_info_page.html
[me@linuxbox ~]$ sys_info_page > sys_info_page.html
[me@linuxbox ~]$ firefox sys_info_page.html
So far, so good.
When writing programs, it’s always a good idea to strive for simplicity and clarity. Main- tenance is easier when a program is easy to read and understand, not to mention that it can make the program easier to write by reducing the amount of typing. Our current ver- sion of the program works fine, but it could be simpler. We could actually combine all the echo commands into one, which will certainly make it easier to add more lines to the pro- gram’s output. So, let’s change our program to this:
First Stage: Minimal Document
#!/bin/bash
# Program to output a system information page
echo "<HTML>
<HEAD>
<TITLE>Page Title</TITLE>
</HEAD>
<BODY>
Page body.
</BODY>
</HTML>"
#!/bin/bash
# Program to output a system information page
echo "<HTML>
<HEAD>
<TITLE>Page Title</TITLE>
</HEAD>
<BODY>
Page body.
</BODY>
</HTML>"
A quoted string may include newlines, and therefore contain multiple lines of text. The shell will keep reading the text until it encounters the closing quotation mark. It works this way on the command line, too:
[me@linuxbox ~]$ echo "<HTML>
[me@linuxbox ~]$ echo "<HTML>
>
>
>
>
>
>
<HEAD>
<TITLE>Page Title</TITLE>
>
>
>
>
>
>
> </HTML>"
> </HTML>"
</HEAD>
<BODY>
</HEAD>
<BODY>
Page body.
Page body.
</BODY>
</BODY>
The leading “>” character is the shell prompt contained in the PS2 shell variable. It ap- pears whenever we type a multi-line statement into the shell. This feature is a little ob- scure right now, but later, when we cover multi-line programming statements, it will turn out to be quite handy.