Shell Scripting Course 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Recap: What are we trying to do?

Sample program: zombie (1)


$ ./zombie 0.005 0.0175 0.01 0.01 500
Scientific computing When Zombies Attack!: Basic Model of outbreak of zombie infection

Population size: 5.0000e+05


Model run time: 1.0e+01 days

i.e. shell scripts that do Zombie destruction rate (alpha): 5.000000e-03

some useful scientific Zombie infection rate (beta):


Zombie resurrection rate (zeta):
1.750000e-02
1.000000e-02

work, e.g. repeatedly Natural death [and birth] rate (delta): 1.000000e-02

running a simulation Output file: zombie.dat

or analysis with different data Model took 7.457018e-02 seconds

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.

Version: Lent 2020 17 Version: Lent 2020 18


This page intentionally left blank
Sample program: zombie (2) Deze bladzijde werd met opzet blanco gelaten.
Simulation of an outbreak of a zombie このページは計画的にブランクを残ている
infection in a closed population Ta strona jest celowo pusta.
blue = Humans Esta página ha sido expresamente dejada en blanco.
red = Zombies
Эта страница нарочно оставлена пустой.
Denne side med vilje efterladt tom.
Ĉi tiu paĝo restas intence vaka.
‫اﯾﻦ ﺻﻔﺤﮫ ﺧﺎﻟﻲ اﺳﺖ‬
Photo: Melbourne Zombie Shuffle by Andrew Braithwaite
An leathanach seo fágtha folamh in aon turas.
Licensed under CC BY 2.0
http://www.flickr.com/photos/bratha/2578784637/

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.

Version: Lent 2020 19 Version: Lent 2020 20


Solution to Part One
Exercise from Day Two (Part One) #!/bin/bash
set -e

# Set up environment variables for program


export ZOMBIE_FORMAT="NORMAL"

Improve the run_program function in # Location of gnuplot file


gnuplot_file="$(pwd –P)/zombie.gplt"

multi-run-while so that as well as function run_program()
{

running zombie it also runs gnuplot # Run program with passed arguments

"$program" "$@" > "stdout-$1-$2-$3-$4-$5"


(using the zombie.gplt file) to plot a # Run gnuplot

graph of the output. gnuplot "$gnuplot_file"

# 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).

2. Immediately after running zombie, run gnuplot:


You can test that this script works by doing the following:
gnuplot "${gnuplot_file}" $ cd
$ rm –f *.dat *.png stdout-* logfile
3. Rename the zombie.png file produced by gnuplot along the same lines as
the zombie.dat file produced by zombie is renamed.
$ cat scripts/param_set | scripts/multi-run-while
$ ls
This exercise highlights one of the advantages of using functions: we can improve or
change our functions whilst leaving the rest of the script unchanged. In particular, the
You should see that there is a PNG file for each of the renamed .dat output
structure of the script remains unchanged. This means two things: (1) if there are any files. You should also inspect logfile to see what it looks like now.
errors after changing the script they are almost certainly in the function we changed, and
(2) the script is still doing the same kind of thing (as we can see at a glance) – we’ve just
changed the particulars of one of its functions.
Version: Lent 2020 21 Version: Lent 2020 22

You might also like