Shell Programming using the C shell
==========================================================================

A file that contains a shell programming is often called a shell script,
or just a script.  It must be executable to run, so use the appropriate
chmod command.  For example, suppose "doit" has a shell program:

    % vi doit
    % chmod 0700 doit

The first line must begin with a Cshell comment, which starts with a # in
column 1.  Thereafter, any mixture of comments and commands may appear.

Parameters

    $1, $2, etc.   -- positional parameters
    $0             -- name of the command
    $argv[*]       -- all parameters
    $#argv         -- the number of parameters

Other shell variables begin with $, except in set statement:

    set name = "Mark"
    set dirname = $2
    echo $dirname

Arithmetic set is different.  Use @ (assign):

    @ i = 2
    @ i--
    @ i++
    @ k = ($x - 2) * 4

Shift command:  gets rid of first element of a wordlist (an array)
If no argument, then it shifts argv[]

    shift
    shift names

Arrays

    set name = (mark sally kathy tony)
    echo $name[0] $name[$i]
    set name = ($name doran)            <-- add one to end
    set name = (bobbie $name)           <-- add one to beginning
    $#name                              <-- number of elements in array
    set name = ($name[1-$k] $name[$j-])   <-- remove middle element of $name
    $name[10-]                          <-- elements 10 to end
    $name[1-6]                          <-- elements 1 to 6
    $name[*]                            <-- all elements of name
    set z = ($z)                        <-- turn $z's value into an array

Is a variable set or not?  (Is it defined?)

    $?var                         Can be used in if or while statements

Special variable

    $$                            Number of current process, can be used to
                                  generate unique names:   /tmp/XXX$$

Control structures

    foreach var (wordlist)      |   foreach i (*)
        commands                |       mv $i $i.c
    end                         |   end
                                |
    if (expr) then              |   if (-f $1) then
        commands1               |       cc $1
    else                        |   else
        commands2               |       echo "No such file $1"
    endif                       |   endif
                                |
      (You can omit the else clause)
                                |
    while (expr)                |   while ($#argv > 0)
        commands                |       grep $something $argv[1]
    end                         |       shift
                                |   end

Csh file queries (used in ifs and whiles)

    -r file           file exists and is readable by user
    -w file           file is writable by user
    -x file           file is executable by user
    -e file           file merely exists (may be protected from user)
    -o file           file is owned by user
    -z file           file has size 0
    -f file           file is an ordinary file
    -d file           file is a directory
    
Example:

     if (-d $name[$i]) then
         echo $name[$i] is a directory
     else
         echo $name[$i] is an ordinary file or does not exist
     endif

Input and output:

     echo         -- generally used for output in shell commands
     echo -n      -- does NOT print newline, useful for writing to same line

     set x = $<   -- $< is the general symbol for "get something from user"
                     Shell script pauses and waits.  Probably write prompt first

Here document:  -- useful for creating a sort of file right in the shellscript

     cat > tempdata <=
     >      (similar)
     <

Debugging

     csh -vx somescript args             v is verbose, x echos the substituted
                                         command
     csh -n somescript                   n says parse without execution to
                                         check the syntax

     Otherwise, you just have to put echo statements in your script!