Shell programming gives the shell a tremendous amount of power.
Not only can groups of commands be used together in a shell program
(or script), but the Bourne shell comes with a full programming
language that allows such things as arithmetic calculations on
variables, decision making or branching, and controlled looping.
Unlike languages such as FORTRAN, PASCAL and C, shell scripts do not
require compiling or linking.
This means that there is no conversion of the source code down into
machine language before execution.
A script is really nothing more than a sequential list of instructions
(some of which are UNIX commands) that the shell interprets and
executes.
Each shell script is written using the particular syntax, or language,
given in the following sections.
After the script has been written to the users satisfaction, it is
made to be an exectabule file using the chmod
command.
If, for example, a user had a file called my_script
that
contained a shell program he would do the following:
chmod u+x my_script
Notice that there is no special extension or any other feature to
distinguish the script as a special program.
A shell script can be given any name with any extension, and therefore
it is convenient to give the scripts meaningful names to help the user
remember their purpose.
A script can also be run in a subshell by using the command
sh script_name
.
The most simple shell script is one which contains a group of UNIX
commands to carry out a particular task.
The following simple example is a script to backup all the files in
the subdirectories above a directory named data.
It first archives all of the subdirectories and files using the
tar
utility, it then compresses the archive file to conserve space, and
finally copies the compressed archive to a remote disk for storage:
#! /bin/sh # Backup is a sh script to back up all of my data tar cfv mydata.tar data/ compress mydata.tar cp mydata.tar.Z /dsk2/storage/
All but the top two lines of the shell script are ordinary UNIX utilities. The second line does nothing, it is just a comment statement. In UNIX shell scripts, the hash character signifies a comment statement, allowing text documentation to be left for future reference. This particular script is short and straightforward enough that documentation may not be required, but in larger scripts it can be essential in order to understand what the program does. Very few people can remember what exactly a program does more than a week after writing it. A well documented script is much nicer to debug, or modify, at a later date than a seemingly endless string of commands and other instructions. The first line is a special line that should be present at the beginning of any shell script written in any shell scripting language. The first two characters tell the shell that what follows is the path of the shell that the script was written under. The current shell will then start a subshell of the type contained in the first line (a Bourne shell in this example). After the script has been executed, control is returned to the original shell. After a glance over the above script, the reader may wonder what the difference is between a shell script and a group of commands, say a pipeline. Well, in this simple example there is not much difference, except for the fact that the same group of commands can be carried out by typing the name of the script rather than re-entering them each time. This is the main reason for putting a small group of commands into a script.