Professional Documents
Culture Documents
Shell Scripting Course 2
Shell Scripting Course 2
Shell Scripting Course 2
work, e.g. repeatedly Natural death [and birth] rate (delta): 1.000000e-02
scientific-computing@uis.cam.ac.uk Simple Shell Scripting for Scientists: Day Three 17 scientific-computing@uis.cam.ac.uk Simple Shell Scripting for Scientists: Day Three 18
Recall the name of this course (“Simple Shell Scripting for Scientists”) and its The zombie program is in your home directory. It is a program written specially
purpose: to teach you, the scientist, how to write shell scripts that will be useful for this course, but we’ll be using it as an example program for pretty general
for your scientific work. tasks you might want to do with many different programs. Think of zombie as
just some program that takes some input on the command line and then produces
some output (on the screen, or in one or more files, or both), e.g. a scientific
As mentioned on the first day of the course, one of the most common (and best) simulation or data analysis program.
uses of shell scripts is for automating repetitive tasks. Apart from the sheer
tediousness of typing the same commands over and over again, this is exactly The zombie program takes 5 numeric arguments on the command line: 4
the sort of thing that human beings aren’t very good at: the very fact that the task positive floating-point numbers and 1 positive integer. It always writes its output
is repetitive increases the likelihood we’ll make a mistake (and not even notice to a file called zombie.dat in the current working directory, and also writes
at the time). So it’s much better to write (once) – and test – a shell script to do it some informational messages to the screen.
for us. Doing it via a shell script also makes it easy to reproduce and record
what we’ve done, two very important aspects of any scientific endeavour. The zombie program is not as well behaved as we might like (which, sadly, is
also typical of many programs you will run). The particular way that zombie is
not well behaved is this: every time it runs it creates a file called running-
So, the aim of this course is to equip you with the knowledge and skill you need zombie in the current directory, and it will not run if this file is already there
to write shell scripts that will let you run some program (e.g. a simulation or data (because it thinks that means it is already running). Unfortunately, it doesn’t
analysis program) over and over again with different input data and organise the remove this file when it has finished running, so we have to do it manually if we
output sensibly. want to run it multiple times in the same directory.
scientific-computing@uis.cam.ac.uk Simple Shell Scripting for Scientists: Day Three 19 scientific-computing@uis.cam.ac.uk Simple Shell Scripting for Scientists: Day Three 20
The zombie program uses a variant of the SIR model from epidemiology to This page intentionally left blank: nothing to see here.
simulate an outbreak of a zombie infection in a closed (i.e. no one enters or leaves)
population. Obviously, since zombies don’t actually exist, it would be a mistake
to try and take this program too seriously. You should think of zombie as just a
program that takes some input on the command line and then produces some
output on the screen and in a file, and whose output can then be fed to yet other
programs for further processing (as we’ll see later this afternoon).
However, as it happens, the program is based on actual academic modelling of the
spread of disease, as found in Chapter 4 (pp. 133-150) of Infectious Disease
Modelling Research Progress (2009), which is entitled “When Zombies Attack!:
Mathematical Modelling of an Outbreak of Zombie Infection”, and which you can
find here:
http://mysite.science.uottawa.ca/rsmith43/zombies.pdf
And in case you are interested in the book from which that chapter is taken, the
ISBN of Disease Modelling Research Progress is 978-1-60741-347-9, it’s edited
by J. M. Tchuenche & C. Chiyaka and published by Nova Science Publishers, Inc.
Note that the zombie program writes its output to a file of numbers rather than
producing graphical output. At the end of this afternoon we will see how to
produce graphs of its output.
running zombie it also runs gnuplot # Run program with passed arguments
…
# Rename files
…
mv zombie.dat "zombie-$1-$2-$3-$4-$5.dat"
mv zombie.png "zombie-$1-$2-$3-$4-$5.png"
…
# Write to logfile
echo "Output file: zombie-$1-$2-$3-$4-$5.dat" >> "$log_file"
echo "Plot of output file: zombie-$1-$2-$3-$4-$5.png" >> "$log_file"
…
}
scientific-computing@uis.cam.ac.uk Simple Shell Scripting for Scientists: Day Three 21 scientific-computing@uis.cam.ac.uk Simple Shell Scripting for Scientists: Day Three 22
The multi-run-while shell script (in the scripts subdirectory of your home If you examine the multi-run-while script in the scripts subdirectory
directory) runs the zombie program (via a shell function called run_program) once of your home directory, you will see that it has been modified as shown above
for each parameter set that it reads in from standard input. This exercise requires you to to run gnuplot after running zombie.
modify the run_program shell function of that script so that, as well as running the
zombie program it also runs gnuplot to turn the output of the zombie program into a
graph. You should be able to tell what all the highlighted parts of the shell script
above do – if there is anything you don’t understand, or if you had any
One sensible way of doing this would be as follows:
difficulty with this part of the exercise, please let the course giver or
1. Figure out the full path of the zombie.gplt file. Store it a shell variable demonstrator know.
(maybe called something like gnuplot_file).