skip to content
 

This is an exercise for you to try once you have attended the third day of the Simple Shell Scripting for Scientists course.

The course notes for this day of the course are available (as a PDF) here.

This exercise was designed to be done on MCS Linux. However, it should work on most modern Linux distributions that have Python 2.7, gnuplot 4.4 or higher (with PNG support), and Eye of GNOME 2.9.0 or higher (or another appropriate PNG viewer).

If you don't do this exercise on on MCS Linux then you should have a look at the compatibility notes before starting the exercise.

Obtaining the files for this exercise

On MCS Linux

In order to do this exercise, you'll need to get hold of the files used in class. If you are doing this on MCS Linux, then after you have logged in, type the following:

cd "${UX}"/Lessons/Shell-Scripting-Scientists
./setup-day-three.sh

You'll be asked for a directory into which the files should be placed, the files will be unpacked to that directory and then set up for you. Look at the README file in the directory to which the files have been unpacked for instructions on how to run the script constructed in class.

On another machine

You will need to download the course files archive to your computer.  You'll also need to unpack the archive and set things up with this setup-day-three.sh script.

Download the archive and setup-day-three.sh script to the same directory. cd to the directory into which you downloaded those two files. Make sure the setup-day-three.sh script is executable by typing:

chmod +x setup-day-three.sh

and then run it by typing:

./setup-day-three.sh

The setup-day-three.sh script will ask you for a directory into which it should unpack the archive and will then set everything up for you.

Once the archive is successfully unpacked, see the README file for details of what it contains.

The exercise

The point of this exercise is to consolidate everything you've learnt over the course thus far. To that end you should write your own shell script FROM SCRATCH for this exercise - do not just take one of the ones constructed in this course and change the names of the programs it runs. Whilst you could certainly get an answer to this exercise that way, you wouldn't learn very much.

You should make your shell script as good a shell script as you can possibly make it. Even if you never bother to be this meticulous ever again when writing your own shell scripts, your understanding of shell scripting will be greatly improved if your answer to this exercise is a carefully thought out, robust shell script that follows the guidelines you have met in this course.

In other words, the shell script you write in answer to this exercise should:

  • be well structured using shell functions,

  • be fully commented,

  • do some error handling,

  • keep a log file of what it is doing,

  • print its error messages on standard error,

  • use a temporary directory for working in,

  • do some checking of its input,

…and so on.

In the archive you will find a program called infect which is a simulation of the spread of infection in a closed population using a variant of the SIR model used in epidemiology.  infect takes three floating point command line arguments and one integer command line argument.

In the gnuplot subdirectory there is a file of gnuplot commands called infect.gplt that can be used to plot the data produced by infect. The commands in this file expect their input to be in a file called infect.dat in the current directory, and they produce a PNG file called infect.png (also in the current directory).

Write a shell script that will read the first three parameters for infect from standard input and the fourth parameter from the command line. It should run the infect program, turning its output into a graph using gnuplot.

Here's an example that illustrates how your shell script should combine the arguments it reads from standard input with the ones it obtains from the command line:

Suppose the script reads the following values from standard input:

1.0 0.1 0.0005 70
2.0 0.1 0.0005 250

…and the values 100 800 from the command line, then your script should run:

infect 1.0 0.1 0.0005 100

infect 1.0 0.1 0.0005 800

infect 2.0 0.1 0.0005 100

infect 2.0 0.1 0.0005 800

There is a file in the scripts subdirectory called infect_params that you can use as a source of parameters to read via standard input. For the command line arguments you can use:

75 100 300 3000 50000

See the course notes for a more detailed description of this exercise.

Compatibility notes

If you are not doing this exercise under MCS Linux then you should be aware of the following issues which may arise when using other Linux/Unix systems:

  • The infect and zombie programs require Python 2 to run. They were written to run under Python 2.7, but should work with recent versions of Python 2 up to Python 2.7.x, and will probably work with any version of Python 2 from Python 2.5 onwards (no promises, though). (Note that the zombie program is not needed for this exercise.)

  • You may have problems if you are using a shell other than bash. You may also have problems if you are using a version of bash earlier than version 2.04. Note that the scripts used in this course were all written to run under bash 4.1, but it is believed that they will run under bash 2.04 or higher (no promises, though). At least three of the scripts used in this course (hello-functionfunction-exits and function-should-exit) won't run properly under versions of bash earlier than 2.04 (these scripts aren't needed for this exercise, though).

  • You will have problems if you are using a version of mktemp earlier than version 1.3, as versions of mktemp prior to version 1.3 did not support the  ‑t option. In particular, versions of mktemp derived from BSD (such as that provided with MacOS X) do not support this option. If you want to use the scripts from this course with such versions of mktemp you will need to modify the scripts to explicitly create files and directories in /tmp instead of using the  ‑t option.

  • The gnuplot commands used have only been tested with gnuplot 4.4. They will probably work with gnuplot 4.0 or higher, but this has not been tested.

  • The version of gnuplot you use must have PNG support. This usually depends on how your version of gnuplot has been compiled. To find out if it has PNG support, start gnuplot and type the following:

    show version long

    If under Compile options either +PNG or +GD_PNG is listed, then your version of gnuplot has been compiled with PNG support.

  • If you do not have Eye of GNOME (eog) on your system, almost any other PNG viewer will do. Most modern web browsers can view PNG files. A list of some applications that support the PNG format is given here.