next up previous contents index
Next: Filters Up: UNIX Shells Previous: Pipes

Shell Scripts

A shell script   is the UNIX equivalent of the VM/CMS EXEC file or the VMS DCL procedure although you should be aware that individual shell commands have limits in the size of their argument strings.

The UNIX shell can be used as an interpretive programming language. Within the shell you can, besides executing shell commands, create and use variables; process (read) arguments; test, branch, and loop; do I/O; etc.

A shell script is a file containing a sequence of commands which can be executed by the shell. The easiest way to execute a script is by typing in the filename on the command line. The shell then interprets and executes the commands in the file one by one.

Although you can write complex programs using the shell language, you can also use simple shell scripts for running complex commands or a series of commands that you use frequently.

The most commonly-used shells in our environment are the C shell and the Bourne shell, or derivatives of these. Many people believe that although the C shell  is better for interactive use, the Bourne shell  is better for scripts, so you may see many scripts written in the Bourne shell.

C shell scripts should begin with a comment   line (a line which begins with the character #). Scripts to be executed by other shells should contain a first line beginning with the character string # followed by the path name of the shell.

Note that in order to execute a script, the file containing it must have execute permission (see Sections gif and following) and the C shell may need to rebuild its table of commands.

 
               		  chmod a+x  mycommand

./mycommand

It is important to remember that, like all UNIX commands that are not built-in to the shell, a script file executes in a child shell forked by the parent shell. The shell running the script file retains environment variables of the parent shell as well as those variables defined in the shell startup file for that shell (e.g. .cshrcgif for the C shell) which is executed before the script. However, at the end of the script, control returns to the parent script and any definitions made by the child  process are not passed back to the parent process . If you want to execute commands that affect the current shell, you must use the source command (C shell): 
 
               		  source   script

(For the Bourne shell, use the . command instead.)

Refer to a good UNIX reference manual to learn about shell programming; see the Bibliography at the end of this Guide for references.



next up previous contents index
Next: Filters Up: UNIX Shells Previous: Pipes



Alan Silverman
Wed Apr 12 16:54:02 METDST 1995