Previous | Table of Contents | Next |
The first line of each shell script determines the shell that runsor interpretsthe program. Always identify the interpreting shell on the first line of the script, using the information from Table 16-2.
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.
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.
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.
The following checklist describes the process to follow when writing any shell script:
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 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.
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 |