next up previous contents
Next: Testing and Branching Up: The C Shell Previous: Variable Modifiers

C Shell Programming

Many of the sections leading up to this point in the chapter have hinted at the fact that the material covered in those sections would be better utilized in shell programs (called shell scripts). Indeed shell programming is the most powerful aspect of the C shell (or any shell for that matter). The C shell provides a rich scripting language that, at best, has a slight similarity to the programming language C. Shell scripting languages provide the user with a great many tools for handling everyday tasks around the system and even some less everyday tasks. A shell script can contain Unix commands as well as the shell commands discussed in the previous and following chapters. Unlike compiler based languages, shell scripts are executed by the shell one line at a time. While this will obviously make for slower performance, advantage is gained in the ease of modifying programs without all of the hassle of compiling and linking. All that is required for a shell script to be executed is that it be made executable with the following command:

% chmod u+x script_name

Shell scripts are often written to handle some of the more tedious tasks that a user encounters on a regular basis. A simple C shell script could be a list of Unix commands that archives and compresses the users home directory and copies it to a specified mounted disk partition for storage gif:

# backup tars and compresses ~/ and puts in storage on /dsk2/strg/
tar -cvf dec18_95.nbdat.tar ~/
compress dec18_95.nbdat.tar
cp dec18_95.nbdat.tar.Z /dsk2/strg/

With the exception of the lines that start with hash marks (#), the script is a list of simple Unix commands. This task could most certainly have been entered on a single command line with use of a pipeline, but it illustrates the basic format of a C shell script. Almost any line starting with a hash mark will be ignored by the shell and hence indicate programmer comments. The one exception to this rule is the hash bang (#!) sequence of characters, this has special meaning to the shell. It tells the shell which environment to start for execution of the script. This could be any shell or even other scripting environments such as perl (Practical Extraction and Report Language) [1] or tcl (Tool Command Language) [2] which are Unix scripting languages but not shells (at least not interactive shells like those discussed in this book). The shells are usually found in the /bin directory, but this might differ from system to system. The powerful feature of shell scripts over simply writing the commands on a command line is that scripts can contain many types of safety, logging, and other features to provide a worry free and organized working environment. As the scripts in this chapter begin to become more complex, this point should become clear.

next up previous contents
Next: Testing and Branching Up: The C Shell Previous: Variable Modifiers

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998