Information & documentation
Simple Shell Scripting for Scientists, Day Two: Exercises
Simple Shell Scripting for Scientists, Day Two: Exercises
These are exercises for you to try once you have attended the second day of the Simple Shell Scripting for Scientists course. Solutions to this exercise will be examined on the third day of this course, so you must attempt this exercise before then or you may find it very difficult to follow the rest of this course.
The course notes for this day of the course are available (as a PDF) here.
These exercises were designed to be done on MCS Linux. It is recommended that you try them on a MCS Linux system, as that is the environment in which they have been tested. However, they should work on most modern Linux distributions that have Python 2.7 (or any later version of Python 2), 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 on on MCS Linux then you should have a look at the compatibility notes before starting the exercise.
Obtaining the files for these exercises
In order to do these exercises, you'll need to get hold of the files used in class. If you are doing these on MCS Linux, then after you have logged in, type the following:
cd "${UX}"/Lessons/Shell-Scripting-Scientists
./setup-day-two.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 these exercises on MCS 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-two.sh script. Download the archive and setup-day-two.sh script to the same directory. cd to the directory into which you downloaded those two files. Make sure the setup-day-two.sh script is executable by typing:
chmod +x setup-day-two.sh
and then run it by typing:
./setup-day-two.sh
The setup-day-two.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.
Simple Shell Scripting for Scientists, Day Two: Final exercises
On day two of this course we created several scripts that run a program with a given parameter set or run a program several times, once for each of a collection of parameter sets. We've looked at different ways of specifying those parameter sets (on the command line and via standard input). We're now going to look at ways of changine our scripts to make our exploration of the program's parameter space even more versatile.
Change to the directory containing the zombie.py program and the answers and scripts subdirectories. First of all, make sure that the multi-run-while.sh shell script in the scripts subdirectory has been modified as we did during the course. You can do this by overwriting the copy in the scripts subdirectory with the copy provided in the answers subdirectory like this:
cp -pf answers/multi-run-while.sh scripts/multi-run-while.sh
(You only need to to do this once.)
There are three parts to this exercise, which you should attempt in the order given:
-
Improve the
run_programfunction inmulti-run-while.shso that as well as runningzombie.pyit also runsgnuplot(using thezombie.gpltfile) to plot a graph of the output. See the course notes for one approach to this task. -
Now create a new shell script based on
multi-run-while.shthat will runzombie.pythree times for each parameter set the scriptreads in on standard input, changing the fifth parameter each time as follows:For a given parameter set a b c d e, first your script should run
zombie.pywith the parameter set:a b c d 50
…then with the parameter set:
a b c d 500
…and then with the parameter set:
a b c d 5000
See the course notes for a more detailed description of this task.
-
Now create a new shell script, based on the script you created in the previous part of the exercise, that does the following:
Instead of running
zombie.pythree times for each parameter set itreads in, this script should accept a set of values on the command line, and use those instead of the hard-coded 50, 500, 5000 previously used.Thus, for each parameter set it
reads in on standard input, it should runzombie.pysubstituting, in turn, the values from the command lineforthe fifth parameter in the parameter set it hasreadin.So, if the script from the previous part of the exercise was called
multi-50-500-5000.sh, and we called this new scriptmulti-sizes.sh(and stored both in thescriptssubdirectory), then - assuming we are in the directory containing thezombie.pyprogram and thescriptssubdirectory - running our new script like this:cat scripts/param_set | scripts/multi-sizes.sh 50 500 5000should produce exactly the same output as running the script you wrote in the previous part of this exercise with the same input file:
cat scripts/param_set | scripts/multi-50-500-5000.shSee the course notes for a hint on how to do this part of the exercise.
(If you can't see the point of the shell scripts you've been asked to write for the last two parts of this exercise, see the course notes for a possible scenario in which they would be useful.)
We're going to be looking at the solutions to the above exercise on the next day of this course, so it is essential that you attempt the exercise before then.
Compatibility notes
If you are not doing these exercises under MCS Linux then you should be aware of the following issues which may arise when using other Linux/Unix systems:
-
The version of the
zombie.pyprogram provided in this archive is written in Python 2. It has only been tested with Python 2.7, although it will probably work with any version of Python 2 from Python 2.5 onwards (no promises, though). -
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 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 these exercises, though). -
You will have problems if you are using a version of
mktempearlier than version 1.3, as versions ofmktempprior to version 1.3 did not support the‑toption. In particular, versions ofmktempderived 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 ofmktempyou will need to modify the scripts to explicitly create files and directories in/tmpinstead of using the‑toption. -
The
gnuplotcommands used have only been tested withgnuplot4.4. They will probably work withgnuplot4.0 or higher, but this has not been tested. -
The version of
gnuplotyou use must have PNG support. This usually depends on how your version ofgnuplothas been compiled. To find out if it has PNG support, startgnuplotand type the following:show version longIf under
Compile optionseither+PNGor+GD_PNGis listed, then your version ofgnuplothas 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.
