next up previous contents index
Next: D. Directory Structure Up: C. Shell Scripts Previous: C.1 C Shell Scripts   Contents   Index


C.2 Parseargs C-shell scripts

Writing user friendly shell scripts often requires a fair amount of administrative work, especially if the number of variables to the script is variable.

If the public domain program parseargs is available, that approach may used. Shell scripts created with this option have the syntax (using the example plummer.csh C-shell script below):

    % plummer.csh [-n <nbody>] [-s <seed>] <file>
in the short notationC.3using single character options, or in the longer form:
    % plummer.csh [+nbody=<nbody>] [+seed=seed>] <file>
In this longer form the difference between the normal NEMO user interface is that keywords have to be preceded with a + symbol, plus it is possible to create scripts with ``parameters'' without an associated keyword. Perhaps this last option should not be used (this example uses it in the <file> filename parameter) in order to keep in pace with core NEMO programs.

Example script plummer.csh:

#!/bin/csh -f
#
#  Example C-shell script, using parseargs, to create an Nbody Plummer model
#
set NAME="`basename $0`"

setenv ARGUMENTS "\
  '?', ARGHIDDEN, argUsage, NULL,    'Help {print usage and exit}', \
  'n', ARGOPT,    argInt,   nbody,   'Nbody {# bodies in disk}', \
  's', ARGOPT,    argInt,   seed,    'Seed {Random number seed}', \
  ' ', ARGREQ,    argStr,   file,    'File {Output Filename}', \
  ' ', ARGLIST,   argStr,   argv,    'argv {any remaining arguments}', \
  ENDOFARGS \
"
## set defaults ##
set nbody='1000'    ## default number of bodies in disk
set seed='0'        ## default inityial seed
set other=()        ## remaining parameter (ignored in here)

## parse command-line ##
parseargs -s csh -e ARGUMENTS -u -- "$NAME" $argv:q >/tmp/tmp$$
if ( $status != 0 ) then  ## improper syntax (or just wanted usage)
        rm -f /tmp/tmp$$
        exit 2
endif

## evaluate output from parseargs & remove temporary file
source /tmp/tmp$$
rm -f /tmp/tmp$$

## echo arguments ##
echo "ARGUMENTS:"
echo "=========="
echo file=$file:q
echo nbody=$nbody:q
echo seed=$seed:q
if ($#argv > 0) echo Additional Positional Parameters=$argv:q

## get down to the work
#
echo " ### Making initial conditions"
mkplummer out=$file nbody=$nbody seed=$seed


(c) Peter Teuben