Here Scripts

Here Scripts

In the following lessons, we will construct a useful application. This application will produce an HTML document that contains information about your system. I spent a lot of time thinking about how to teach shell programming, and the approach I have come up with is very different from most approaches that I have seen. Most favor a rather systematic treatment of the many features, and often presume experience with other programming languages. Although I do not assume that you already know how to program, I realize that many people today know how to write HTML, so our first program will make a web page. As we construct our script, we will discover step by step the tools needed to solve the problem at hand.

Writing an HTML file with a script

As you may know, a well formed HTML file contains the following content:

<HTML>
<HEAD>
<TITLE>
The title of your page
</TITLE>
</HEAD>

<BODY>
Your page content goes here.
</BODY>
</HTML>

Now, with what we already know, we could a write a script to produce the above content:

#!/bin/bash

# make_page - A script to produce an HTML file

echo "<HTML>"
echo "<HEAD>"
echo " <TITLE>"
echo " The title of your page"
echo " </TITLE>"
echo "</HEAD>"
echo ""
echo "<BODY>"
echo " Your page content goes here."
echo "</BODY>"
echo "</HTML>"

This script can be used as follows:

[me@linuxbox me]$ make_page > page.html

It has been said that the greatest programmers are also the laziest. They write programs to save themselves work. Likewise, when clever programmers write programs, they try to save themselves typing.

The first improvement to this script will be to replace the repeated use of the echo command with a here script, thusly:

#!/bin/bash

# make_page - A script to produce an HTML file

cat << _EOF_
<HTML>
<HEAD>
<TITLE>
The title of your page
</TITLE>
</HEAD>

<BODY>
Your page content goes here.
</BODY>
</HTML>
_EOF_

A here script (also sometimes called a here document) is an additional form of I/O redirection. It provides a way to include content that will be given to the standard input of a command. In the case of the script above, the cat command was given a stream of input from our script to its standard input.

A here script is constructed like this:

command << token
content to be used as command's standard input
token

token can be any string of characters. I use "_EOF_" (EOF is short for "End Of File") because it is traditional, but you can use anything, as long as it does not conflict with a bash reserved word. The token that ends the here script must exactly match the one that starts it, or else the remainder of your script will be interpreted as more standard input to the command.

There is one additional trick that can be used with a here script. Often you will want to indent the content portion of the here script to improve the readability of your script. You can do this if you change the script as follows:

#!/bin/bash

# make_page - A script to produce an HTML file

cat <<- _EOF_
<HTML>
<HEAD>
<TITLE>
The title of your page
</TITLE>
</HEAD>

<BODY>
Your page content goes here.
</BODY>
</HTML>
_EOF_

Changing the the "<<" to "<<-" causes bash to ignore the leading tabs (but not spaces) in the here script. The output from the cat command will not contain any of the leading tab characters.

O.k., let's make our page. We will edit our page to get it to say something:

#!/bin/bash

# make_page - A script to produce an HTML file

cat <<- _EOF_
<HTML>
<HEAD>
<TITLE>
My System Information
</TITLE>
</HEAD>

<BODY>
<H1>My System Information</H1>
</BODY>
</HTML>
_EOF_

In our next lesson, we will make our script produce real information about the system.

Substitutions - Part 1

#!/bin/bash

# make_page - A script to produce an HTML file

cat <<- _EOF_
<HTML>
<HEAD>
<TITLE>
My System Information
</TITLE>
</HEAD>

<BODY>
<H1>My System Information</H1>
</BODY>
</HTML>
_EOF_

Now that we have our script working, let's improve it. First off, we'll make some changes because we want to be lazy. In the script above, we see that the phrase "My System Information" is repeated. This is wasted typing (and extra work!) so we improve it like this:

#!/bin/bash

# make_page - A script to produce an HTML file

title="My System Information"

cat <<- _EOF_
<HTML>
<HEAD>
<TITLE>
$title
</TITLE>
</HEAD>

<BODY>
<H1>$title</H1>
</BODY>
</HTML>
_EOF_

As you can see, we added a line to the beginning of the script and replaced the two occurrences of the phrase "My System Information" with $title.

Variables

What we have done is to introduce a very fundamental idea that appears in almost every programming language, variables. Variables are areas of memory that can be used to store information and are referred to by a name. In the case of our script, we created a variable called "title" and placed the phrase "My System Information" into memory. Inside the here script that contains our HTML, we use "$title" to tell the shell to substitute the contents of the variable.

As we shall see, the shell performs various kinds of substitutions as it processes commands. Wildcards are an example. When the shell reads a line containing a wildcard, it expands the meaning of the wildcard and then continues processing the command line. To see this in action, try this:

[me@linuxbox me]$ echo *

Variables are treated in much the same way by the shell. Whenever the shell sees a word that begins with a "$", it tries to find out what was assigned to the variable and substitutes it.

How to create a variable

To create a variable, put a line in your script that contains the name of the variable followed immediately by an equal sign ("="). No spaces are allowed. After the equal sign, assign the information you wish to store. Note that no spaces are allowed on either side of the equal sign.

Where does the variable's name come from?

You make it up. That's right; you get to choose the names for your variables. There are a few rules.

  1. It must start with a letter.
  2. It must not contain embedded spaces. Use underscores instead.
  3. Don't use punctuation marks.
  4. Don't use a name that is already a word understood by bash. These are called reserved words and should not be used as variable names. If you use one of these words, bash will get confused. To see a list of reserved words, use the helpcommand.

How does this increase our laziness?

The addition of the title variable made our life easier in two ways. First, it reduced the amount of typing we had to do. Second and more important, it made our script easier to maintain.

As you write more and more scripts (or do any other kind of programming), you will learn that programs are rarely ever finished. They are modified and improved by their creators and others. After all, that's what open source development is all about. Let's say that you wanted to change the phrase "My System Information" to "Linuxbox System Information." In the previous version of the script, you would have had to change this in two locations. In the new version with the title variable, you only have to change it in one place. Since our script is so small, this might seem like a trivial matter, but as scripts get larger and more complicated, it becomes very important. Take a look at some of the scripts in the Script Library to get a sense of what large scripts look like.

Environment Variables

When you start your shell session, some variables are already ready for your use. They are defined in scripts that run each time a user logs in. To see all the variables that are in your environment, use the printenv command. One variable in your environment contains the host name for your system. We will add this variable to our script like so:

#!/bin/bash

# make_page - A script to produce an HTML file

title="System Information for"

cat <<- _EOF_
<HTML>
<HEAD>
<TITLE>
$title $HOSTNAME
</TITLE>
</HEAD>

<BODY>
<H1>$title $HOSTNAME</H1>
</BODY>
</HTML>
_EOF_

Now our script will always include the name of the machine on which we are running. Note that, by convention, environment variables names are uppercase.

 

A Guided Tour

It's time to take our tour. The table below lists some interesting places to explore. This is by no means a complete list, but it should prove to be an interesting adventure. For each of the directories listed below, do the following:

  • cd into each directory.
  • Use ls to list the contents of the directory.
  • If you see an interesting file, use the file command to determine its contents.
  • For text files, use less to view them.
Interesting directories and their contents
Directory Description
/ The root directory where the file system begins. In most cases the root directory only contains subdirectories.
/boot This is where the Linux kernel and boot loader files are kept. The kernel is a file called vmlinuz.
/etc The /etc directory contains the configuration files for the system. All of the files in /etc should be text files. Points of interest:
/etc/passwd
The passwd file contains the essential information for each user. It is here that users are defined.
/etc/fstab
The fstab file contains a table of devices that get mounted when your system boots. This file defines your disk drives.
/etc/hosts
This file lists the network host names and IP addresses that are intrinsically known to the system.
/etc/init.d
This directory contains the scripts that start various system services typically at boot time.
/bin, /usr/bin These two directories contain most of the programs for the system. The /bin directory has the essential programs that the system requires to operate, while /usr/bin contains applications for the system's users.
/sbin, /usr/sbin The sbin directories contain programs for system administration, mostly for use by the superuser.
/usr The /usr directory contains a variety of things that support user applications. Some highlights:
/usr/share/X11
Support files for the X Windows system
/usr/share/dict
Dictionaries for the spelling checker. Bet you didn't know that Linux had a spelling checker. See look and ispell.
/usr/share/doc
Various documentation files in a variety of formats.
/usr/share/man
The man pages are kept here.
/usr/src
Source code files. If you installed the kernel source code package, you will find the entire Linux kernel source code here.
/usr/local /usr/local and its subdirectories are used for the installation of software and other files for use on the local machine. What this really means is that software that is not part of the official distribution (which usually goes in /usr/bin) goes here.

When you find interesting programs to install on your system, they should be installed in one of the /usr/local directories. Most often, the directory of choice is /usr/local/bin.
/var The /var directory contains files that change as the system is running. This includes:
/var/log
Directory that contains log files. These are updated as the system runs. You should view the files in this directory from time to time, to monitor the health of your system.
/var/spool
This directory is used to hold files that are queued for some process, such as mail messages and print jobs. When a user's mail first arrives on the local system (assuming you have local mail), the messages are first stored in /var/spool/mail
/lib The shared libraries (similar to DLLs in that other operating system) are kept here.
/home /home is where users keep their personal work. In general, this is the only place users are allowed to write files. This keeps things nice and clean :-)
/root This is the superuser's home directory.
/tmp /tmp is a directory in which programs can write their temporary files.
/dev The /dev directory is a special directory, since it does not really contain files in the usual sense. Rather, it contains devices that are available to the system. In Linux (like Unix), devices are treated like files. You can read and write devices as though they were files. For example /dev/fd0 is the first floppy disk drive, /dev/sda (/dev/hda on older systems) is the first IDE hard drive. All the devices that the kernel understands are represented here.
/proc The /proc directory is also special. This directory does not contain files. In fact, this directory does not really exist at all. It is entirely virtual. The /proc directory contains little peep holes into the kernel itself. There are a group of numbered entries in this directory that correspond to all the processes running on the system. In addition, there are a number of named entries that permit access to the current configuration of the system. Many of these entries can be viewed. Try viewing /proc/cpuinfo. This entry will tell you what the kernel thinks of your CPU.
/media,/mnt Finally, we come to /media, a normal directory which is used in a special way. The /media directory is used for mount points. As we learned in the second lesson, the different physical storage devices (like hard disk drives) are attached to the file system tree in various places. This process of attaching a device to the tree is called mounting. For a device to be available, it must first be mounted.

When your system boots, it reads a list of mounting instructions in the file /etc/fstab, which describes which device is mounted at which mount point in the directory tree. This takes care of the hard drives, but you may also have devices that are considered temporary, such as CD-ROMs and floppy disks. Since these are removable, they do not stay mounted all the time. The /media directory is used by the automatic device mounting mechanisms found in modern desktop oriented Linux distributions. On systems that require manual mounting of removable devices, the /mnt directory provides a convenient place for mounting these temporary devices. You will often see the directories /mnt/floppy and /mnt/cdrom. To see what devices and mount points are used, type mount.

Member Login| Logout
 
East African Standard Time

Username
Password
Showbiz - Soulja Boy Tell'em - "Kiss Me Thru The Phone"
 
Exclusive
 

Adventure
 
Adventure with us today. Find out more on travel agents and other facilities around East Africa from here. Find out the most fascinating places that you can not miss to visit while in East Africa. Get all this from richcomputersolutions - your favorite website. Remember if you want to join this website, suggest a user name and password and send them to richymany2k@gmail.com or click on sin-up for more. Joining this website is a 100% free. Remember that we offer the best in repair of computers, scanners , printers. On top of that, reach us for quality photography, internet connections .graphic designing, website designing, coomputer training and many mor services. You are welcome to the sole source of information in East Africa.
Advertise here
 
 
There have been 39224 visitors (169175 hits) To this Website today
I welcome you all to his website, for all the best you need and enjoy all the benefits as a member. This website website is still under construction but always hope to find more here with Rich Computer Solutions. Always come back for more events. This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free