\subsection{Shell Programming}

\subsubsection{Shell Scripts}

A shell script is a program written in the shell's command language.
This language is composed of (almost)\footnote{Some commands were simply
intended to be used interactively, such as {\tt talk}, and balk when
they are fed input from something that is not a tty.  I think this is
incorrect behavior, but then again, I like System V too.} any command
you might type interactively, along with control commands such as {\tt
if} and, {\tt endif}.

A sample, silly script:

\begin{verbatim}
#!/bin/csh -f
# the -f option skips the usual step of sourcing the
# .cshrc file, so let's set the path explicitly.
# Note that this script will inherit the path of the program
# which invokes this script, so we are really just being paranoid
# by setting the path here.

set path = (/usr/ucb /bin)

# this is a silly script, but it'll do.

f $1@red.rutgers.edu
f $1@blue.rutgers.edu
f $1@gold.rutgers.edu
\end{verbatim}

\$1 is the name of the first argument you give to any shell command.
These variables exist from 1 to 9: \$1, \$2 ...  \$9.  \$0 is the name
of the program, and \$* stands for {\em all} the arguments (an unlimited
number).

So, this script fingers a username (given as the one argument) at
several different machines.  Here is a slight modification which
demonstrates how to loop through several arguments:
\begin{verbatim}
#\bin/csh -f
foreach person ($*)

f $person@red.rutgers.edu
f $person@blue.rutgers.edu
f $person@gold.rutgers.edu
f $person@topaz.rutgers.edu
f $person@caip.rutgers.edu
f $person@elbereth.rutgers.edu

end
\end{verbatim}

A good way to deal with shell scripts like these is to make
subdirectories called {\em \~{ }/vaxbin} and {\em \~{ }/rtbin}, and put
the scripts in one of them, with symbolic links from the other
directory.  {\Large explain symlinks and MACHTYPE/HOSTTYPE?} Make sure
that you have used {\tt chmod}\footnote{As in {\tt chmod +x} {\em
scriptname}} to make the scripts executable, and that you have modified
your PATH variable so that the shell will search
{\em \~{ }/\$\{HOSTTYPE\}bin}.

\subsubsection{The {\em .login} and {\em .cshrc} files}

The {\it .cshrc} and {\it .login} files are special examples of shell
scripts.  The contents of the {\it .cshrc} file are executed each time
you create a shell.  One obvious example is upon login; another is when
you run xterm to set up another window.  The contents of the {\it
.login} file are run after the {\it .cshrc} file is run, but {\bf only}
in the login window when logging in.

To force the running of the {\em .login} or {\em .cshrc}, use the
command {\tt source}.  The difference between executing and sourcing a
file is that executing a shell script starts up another shell.  When
you're dealing with the {\it .login} and {\it .cshrc} files, the idea is
to get the changes into your current shell.  The {\tt source} command
does this.

{\tt \% source .cshrc ; source .login}

% Local Variables:
% mode: TeX
% version-control: t
% make-backup-files: t
% End:
