skip to primary navigationskip to content
 

Simple Shell Scripting for Scientists

Course notes

If you wish to attend this course, please ensure that you satisfy its prerequisites. (No previous experience of shell scripting is required for this course, however some knowledge of the interactive use of the Bourne-Again SHell (bash) is required; see the prerequisites for details.)

Course description

This course is part of the Scientific Computing series, and as such the examples chosen are of most relevance to scientific computing.

This course introduces shell scripting in the Bourne-Again SHell (bash) for scientific computing tasks. Day one introduces very basic shell scripts in bash which process the command line in a simple fashion. Day two covers how to write more advanced shell scripts in bash. Day three covers how to make one's shell scripts more robust.

At the end of each day of the course one or more exercises are set. It is VERY IMPORTANT that attendees attempt these exercises before the next day of the course. Attendees should make sure that they have allowed themselves sufficient study time for these exercises between each day of the course.

Upon completion of this course, attendees may find it useful to look at the course notes for the “Unix: Introduction to the Command Line Interface”  and “Further use of Unix” courses, as these notes introduce more sophisticated Unix commands that could be used in shell scripts.

The course notes are available here.

Day one:

  • Recap of common Unix/Linux commands
  • What is a shell script and how does it work?
  • Errors in shell scripts
  • Writing very simple shell scripts
  • Comments in shell scripts
  • Simple use of shell builtin commands: cdechoexportpwdset
  • Simple use of shell variables and parameters
  • Simple processing of the command line
  • Redirecting standard output
  • Shell flow of control constructs: for
  • Translating your task into a shell script
  • Writing more useful shell scripts

Day two:

  • Solution to exercise from previous day
  • Structuring shell scripts
  • Testing and debugging shell scripts
  • Simple use of shell builtin commands: pwdreadtest
  • Shell flow of control constructs: while
  • Shell functions
  • Command substitution
  • The mktemp command
  • Obtaining input from the keyboard/standard input
  • Pipelines
  • Arithmetic expansion
  • Translating your task into a shell script
  • Writing more advanced shell scripts

Day three:

  • Solution to exercises from previous day
  • Handling errors in shell scripts
  • Simple use of shell builtin commands: exitreturnsettest
  • Shell flow of control constructs: if
  • Redirecting standard error
  • Handling filenames
  • Structuring shell scripts
  • Translating your task into a shell script
  • Writing more advanced shell scripts

Day four:

  • Solution to exercise from previous day
  • Simple use of shell builtin commands: localsource
  • Lists of commands: ;&&||
  • Shell flow of control constructs: case
  • Handling filenames
  • More advanced use of shell variables (parameter expansion)
  • More advanced use of tests in if statements
  • More advanced processing of the command line
  • Translating your task into a shell script
  • Writing more advanced shell scripts

Notes

In due course the notes for this course will be made available in their original format. In the meantime they are available here as PDFs.

Day one: Notes (PDF) Exercise and this day's course files
Day two: Notes (PDF) Exercises and this day's course files
Day three: Notes (PDF) Exercise and this day's course files

The course notes featured an appendix with a list of useful Unix/Linux commands. This is available (currently only as a PDF) here.

All comments or questions regarding this course, and any course materials provided, should be sent to .