Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: Wildcard characters Up: Shell Scripts and Awk Previous: Contents   Contents

Shell Programming

Image shellfig
At the heart of Unix is a kernel whose routines aren't easy to use directly. There are also many pre-written programs (ls to list files, etc). They're easier to use, but they don't understand the use of '*' as a wildcard character, and besides, you need some sort of editor when you type commands in if you're going to use the command line. The `shell' is the interface between the user and the system. The shell isn't only a line editor and a command line interpreter that understands wildcards, it's also a language with variables, arrays, functions and control structures. Command lines can be put into a file and executed. These so-called ``shell scripts'' can quickly be written and tested and should be tried in association with other standard unix utilities before embarking on a higher level language, at least for prototyping purposes.

Various text-based shells are in use. sh, the Bourne Shell, is the oldest. The C-shell (csh) has many useful features lacking from sh but isn't that good for programming in. The Korn Shell (ksh) and the (very similar) POSIX shell are developments of sh that incorporates many csh features. bash is similar and is freely available (it's the default on linux and MacOS X). This document is aimed at bash users on CUED's Teaching System, though non-csh users elsewhere shouldn't have any problems.

Writing a shell script is easy - start up your editor with a file called try then type a few harmless commands into this file, one per line. For example

   hostname
   date
   ls

then save it as text. You want to make this file executable, so in the terminal window type `chmod u+x try', adding eXecute permission for the User. Run this script by typing its name (on some systems you may need to type `./try' - the '.' is shorthand for 'the current directory'). You should see the machine's name, the date and a list of files.

Even scripts as simple as this can save on repetitive typing, but much more can be easily achieved.



Subsections
next up previous contents
Next: Wildcard characters Up: Shell Scripts and Awk Previous: Contents   Contents
Tim Love 2010-04-27