Previous Table of Contents Next


Identifying the Shell

The first line of each shell script determines the shell that runs—or interprets—the program. Always identify the interpreting shell on the first line of the script, using the information from Table 16-2.

Table 16-2 First Line of Script

Shell Syntax
Bourne #!/bin/sh
Korn #!/bin/ksh
C #!/bin/csh -f

The -f (fast) option to /bin/csh runs the script without sourcing the .cshrc file.

If you do not specify the shell in the first line of the script, and it is an executable script, the current shell interprets the contents of the script.

After the first line of a script, any line beginning with a pound sign (#) is treated as a comment line and is not executed as part of the script.

Making Scripts Executable

Before you can run a shell script, it is customary to change its permissions so that it has at least read and execute permissions (chmod 555). When you write and debug the script, give yourself write permission to the file (chmod 755) so that you can edit it. When assigning permissions to a completed shell script, consider the scope of access that you want to permit to this script. Use restrictive permissions if the script is proprietary or individual, and use more relaxed permissions if many users who are not in the same group will use the script.

Storing Shell Scripts

After you create a shell script, you can execute it only from the directory in which it resides or by using the full pathname, unless you set your PATH variable to include the directory that contains the script.

If you write many scripts, you may want to create a ~/bin directory in your home directory and update your search path to include this directory. In this way, your scripts are available regardless of where you are in the directory hierarchy. If you provide scripts for more general use, be sure to debug them before you put them in a directory where they are more accessible.

Writing Shell Scripts: The Process

The following checklist describes the process to follow when writing any shell script:

1.  Decide what you want the script to do. Establish a list of the commands you need to use to accomplish the desired task.
2.  Use an editor to put the commands into a file. Give the file a name that indicates what the script does.
3.  Identify the shell in the first line of the script.
4.  Include comments at the beginning of the script to describe its purpose and to annotate each individual part of the script. These comments can be useful when you debug the script and to interpret a script that may be used only occasionally. Comments are also invaluable in helping others to interpret scripts that you have written.
5.  Save the file and quit the editor.
6.  Change the permissions so that the file has, at a minimum, read and execute permissions.
7.  Check your path, or the PATH variable, to make sure that the directory that contains the script is in the search path.
8.  Type the name of the script as a command. The script executes one line at a time.
9.  If errors occur, debug the script.
10.  When the script is complete, decide where you want to store the command (for example, in your home directory, your local ~/bin directory, or in a more globally available directory).

Variables

A variable is a name that refers to a temporary storage area in memory. A variable holds a value. Changing a variable's value is called assigning a value to the variable. Shell programming uses two types of variables: shell variables and environment variables. By convention, you write shell variables in lowercase and environment variables in uppercase.

Shell Variables

Shell variables are maintained by the shell and are known only to the shell. Shell variables are always local and are not passed on from parent to child processes.

Displaying Variables from a Command Line

You use the set command with no arguments to display a list of current shell and environment variables. The Bourne and Korn shells display variables in the format shown in the following example:

$ set
CALENDAR=/home/winsor
DVHOME=/home/winsor/docudisc/dd.alpha
DVPATH=/home/winsor/docudisc/dd.alpha
ERRNO=1Ø
FCEDIT=/bin/ed
FMHOME=/home2/frame
HELPDIR=$SUNDESK/help
HOME=/home1/winsor
HZ=1ØØ
IFS=

LD_LIBRARY_PATH=/usr/openwin/lib:/usr/lib:/usr/ucblib
LINENO=1
LOGNAME=winsor
MAIL=/var/mail/winsor
MAILCHECK=6ØØ
MANSECTS=\1:1m:1c:1f:1s:1b:2:\3:3c:3i:3n:3m:3k:3g:3e:3x11:3xt:3w:3b:
9:4:5:7:8
OPENWINHOME=/usr/openwin
OPTIND=1
PATH=.:/home1/winsor:/usr/openwin:/usr/openwin/bin/xview:/home2/frame/bin:
/usr/dist/local/exe:/usr/dist/exe:/home1/winsor/bin:/etc:/usr/etc:
/usr/sbin:/usr/bin:/usr/ucb:.
PPID=9139
PS1=$
PS2=>
PS3=#?
PS4=+
PWD=/home1/winsor
RANDOM=23592
SECONDS=1
SHELL=/bin/csh
TERM=sun
TMOUT=Ø
TZ=US/Pacific
USER=winsor
$

The C shell displays its variables in the following format:

[26]castle{winsor}% set
argv    ()
cwd     /home1/winsor
history 25
home    /home1/winsor
ignoreeof
noclobber
noglob
path    (. /home1/winsor /usr/openwin /usr/openwin/bin/xview
/home2/frame/bin /usr/dist/local/exe /usr/dist/exe /home1/winsor/bin /etc
/usr/etc /usr/sbin /usr/bin /usr/ucb .)
prompt  [!]castle{winsor}%
savehist        25
shell   /bin/csh
status  Ø
term    sun
time    15
user    winsor
[27]castle{winsor}%


Previous Table of Contents Next