next up previous contents
Next: Running and Debugging Up: Shell Programming Previous: Which Shell?   Contents

Shell Scripts

Files that contain a set of shell commands are called ``script files''. you don't have to be in the Bourne shell to execute a Bourne shell script. All that is required is that the very first line of the file contains:-

#!/bin/sh
This is a standard trick. The # is a comment character, but the following ! tells the system to use the file name that follows as the shell to run the script and /bin/sh is the Bourne shell processor. This also works on Linux as there sh is equivalent to bash.

A shell runs until:-

  1. it encounters an error.

  2. it reaches the end of the script file.

  3. it reaches an exit command.

Although it works to let a script ``fall through'' to the end, its better to use exit which takes an numeric argument. Use a 0 with it signal that the script is ending normally and a non-zero number otherwise.

Putting this together, the framework for a shell looks something like this:-

#!/bin/sh
# Comments about the script

the script commands

exit 0

If a line is too wide to comfortably fit on the screen it can be extended by placing a \ as the very last character on the line e.g.:-

str="this is a very very very very $\backslash$
very very very very very very long string"

Normally each command is placed on a separate line, but you can put several on the same line if they are separated by ``;''e.g.:-

cd; echo "O.K., I am here now"


next up previous contents
Next: Running and Debugging Up: Shell Programming Previous: Which Shell?   Contents
P.D. Gronbech (IT Staff) 2015-10-02