Unix Support

Simple Shell Scripting for Scientists, Day Three: Exercise

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 PWF Linux. It is recommended that you try it on a PWF Linux system, as that is the environment in which it has been tested. However, it should work on most modern Linux distributions that have Python 2.4, 2.5, 2.6 or 2.7, gnuplot 4.0 or higher (with PNG support), and Eye of GNOME 2.9.0 or higher (or another appropriate PNG viewer), installed.

If you don't do this exercise under PWF Linux you may need to recompile the iterator program (see the README file in the archive of files from the third day of this course for details) if you want to try out the scripts used during the course. You should also have a look at the compatibility notes.

Obtaining the files for this exercise

In order to do this exercise, you'll need to get hold of the files used in class. If you are doing this on PWF 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.

If you are not doing this exercise on PWF Linux, then you'll need to get copies of the files that were used in class onto your own system and set up properly. These files are available in an archive here. 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. This file also gives brief instructions on how to recompile the iterator source code, in case the precompiled version of iterator included in the archive won't run on your system.

Simple Shell Scripting for Scientists, Day Three: Final exercise

The point of this exercise is to consolidate everything you've learnt over all three days of this course. 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 lissajous.py which produces points on a Lissajous curve that it prints to standard output. lissajous.py takes two floating point command line arguments, although in this exercise we'll restrict ourselves to using only integer arguments for it.

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

Write a shell script that will read the first parameter for lissajous.py from standard input and the second parameter from the command line. It should run the lissajous.py 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:

      12
      23

    …and the values 5 9 32 from the command line, then your script should run:

      lissajous.py 12 5

      lissajous.py 12 9

      lissajous.py 12 32

      lissajous.py 23 5

      lissajous.py 23 9

      lissajous.py 23 32

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

    2 4 6 8

 

Compatibility notes

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

  • The lissajous.py program requires Python to run. It was written to run under Python 2.6, but should work with recent versions of Python up to Python 2.7. In fact, it probably will work with any version of Python prior to Python 3.0 (although this has not been tested), and almost certainly with any version of Python 2.x.

  • The compiled version of the iterator program provided in this archive was compiled on an Intel Core 2 Duo Processor E6300 under openSUSE 11.2 (2.6 kernel, GLIBC 2.10.1), using GCC 4.4. It is not guaranteed to work on any other Linux/Unix distribution and/or architecture. If it will not run then you'll have to compile it from source. See the README file in the archive for details. (Note that the iterator program is not actually 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.0, but it is believed that they will run under bash 2.04 or higher (no promises, though). At least one of the scripts used in this course (hello-function.sh) won't run properly under versions of bash earlier than 2.04 (this script isn'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.2. They will probably work with gnuplot 3.7 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.

The title of this document is: Simple Shell Scripting for Scientists - Day Three: Exercises
URL: http://www-uxsup.csx.cam.ac.uk/courses/moved.ShellScriptingSci/exercises/day-three.html