4.2. Writing Scripts

This section provides a quck introduction to script writing. We are not going to describe bash or Tcl. If you need information about those languages, check out the resources at the end of this chapter. What we will describe is how to prepare a script and make it executable. While this is trivial for bash, it can be a bit more challenging for Tcl scripts.

A script is just a file that contains commands in a particular scripting language. In order to run, a script must be interpreted by the appropriate command intepreter (e.g. bash or tclsh). Unix/Linux makes it possible to start the interpretation of a script in exactly the same way you would start a compiled program, by typing its name at the command prompt.

4.2.1. The PATH environment variable

To run a command, any command; the shell must be able to locate the file the implements that command. Linux shells do this by searching the PATH environment variable. PATH is an environment variable that is initially set up by the system wide startup scripts. It consists of a set of colon (:) separated directory paths. Unix shells search for commands in the directory paths in the PATH variable in the order in which they occur in the PATH variable.

When the bash shell starts, it will (usually) source the ~/.bashrc script file automatically. For exceptions to this, see the section about building desktop shortcuts in this chapter. You can use this script to change the value of the PATH variable. Note that since the bash does not have any 'built-in' path, you should usually only add elements to PATH rather than replacing it.

In most environments, the directory ~/bin is part of the path. We therefore strongly recommend that scripts either be installed in that directory or, more maintainably, a soft link be made in the ~/bin directory that points to scripts that are part of your system integration work.

Many users want to be able to run scripts and rograms that are located in their current working directory, by just typing their name rather than having to type ./somescript. You can do this by adding a line to your ~/.bashrc file that appends the directory . to the path, for example:

export PATH=$PATH:.
This line makes the path environment variable equal to its previous value with the text :. appended to it.

4.2.2. Making script files directly executable

Once you have prepared a script file we need to make it directly executable. To do this we must:

In this section we will work with two files, a bash shell script named myscript and a Tcl script named ascript.tcl.

Use the chmod unix command to set the permissions of the scripts to permit execution:

Example 4-1. Using chmod to allow execution

chmod a+x myscript ascript.tcl

The script itself describes the interpreter to use to process it. If the script begins with the special sequence of characters: #! the remainder of that line are considered to be the path of the command that will interpret the file.

Example 4-2. #! lines for bash and for tclsh


Note that the full path to the interpreter must be provided, PATH will not be searched. There are two other pitfalls to keep in mind:

To ensure that our scripts will be located we could do any of the following:

  1. Add the directory containing our scripts to the PATH

  2. Install the script in ~/bin

  3. Make a symbolic link in ~/bin to the script.

The first choice can lead to long PATH commands which can be unwieldy. The second option either loses the logical directory structure you are trying to maintain in your development, or can cause errors if you forget to install a script that has been modified. Therefore, we will make a symbolic link. Suppose the two scripts are located in the directory ~/sillyscripts:

Example 4-4. Making symbolic links in ~/bin

ln -s ~/sillyscripts/myscript    ~/bin/myscript
ln -s ~/sillyscripts/ascript.tcl ~/bin/ascript.tcl

The example above makes the appropriate links.