Previous Table of Contents Next


Example Scripts

These sections contain some examples of Bourne shell scripts.

Anonymous ftp Script

A script named fez performs an anonymous ftpget or list.

#!/bin/sh
#
#    @(#)fez,v1.Ø        (me@anywhere.EBay.Sun.COM) 08/29/92
#
PATHNAME=anywhere.EBay:/home/me/bin/fez
MYNAME=`basename $0`
#
# Author:
#   Wayne Thompson
#
# Synopsis:
usage=`/bin/sed -e "s/^ *//" << endusage
   usage: $MYNAME [-h] [-abfglmprR] [[login@]hostname:[sourcepath]]
endusage`
#
# Description:
#   This script will perform an anonymous ftp get or list.
#
#   If the hostname is not specified on the command line, then
#   it is derived from the basename(1) of the current directory.
#   This provides for a hierarchy of hostnames within which to
#   get files or directory listings.
#
#   If no flags and arguments are given or a single argument of
#   the form "hostname:" is given, the default actionwill be
#   to retrieve a recursive directory listing from the remote
#   host into the file "./ls-lR.".
#
#   Directory listings are named in the following manner:
#     <the command used to produce the listing w/spacescollapsed>
#     followed by the path it was produced from with slashes(/)
#     transliterated to dots(.). e.g.
#       ls-l.             (ls -l /)
#       ls-lR.a.b         (ls -lR /a/b)
#
#   When mirroring is enabled (default), the directory listing
#   "ls-lR.a.b" would be placed in the directory"./a/b" which
#   would be created if necessary.
#   Likewise, "fez export.lcs.mit.edu:/contrib/3dlife.c"would
#   get "/contrib/3dlife.c" from"export.lcs.mit.edu" and place
#   it into "./contrib/3dlife.c".
#   An alternative behavior is provided by the "-f" flag,which
#   serves to flatten the destination hierarchy.
#   "fez -f export.lcs.mit.edu:/contrib/3dlife.c" wouldget
#   "/contrib/3dlife.c" into "./3dlife.c"
#
#   The default user is "anonymous".
#   The default password is one of:
#     internal to Sun = $USER@hostname
#     external to Sun = $USER@Sun.Com
#   You may override these in $HOME/.netrc (see netrc(5))
#   or from the command line.
#
# Options:
#   -a    ascii mode
#   -b    binary mode                        (default)
#   -f    flatten hierarchy
#   -g    get                          (default w/arg)
#   -h    print command description.
#   -l    list                        (default w/o arg)
#   -m     mirror hierarchy                (default)
#   -p passwd password
#   -(r|R) recursive                  (default w/o arg)
#
# Environment:
#
# Files:
#   $HOME/.netrc           file for ftp remote login data
#
# Diagnostics:
#   Exit Status:
#     Ø     normal termination
#     1     abnormal termination
#
#   Errors (stderr):
#     usage
#
#   Warnings (stderr):
#
#   Info (stdout):
#
# Dependencies:
#   Fez only knows how to talk to UNIX(R) hosts.
#
# Caveats:
#   A recursive directory get will not fetch subdirectories unless
#   they already exist.
#
# Bugs:
#
# >> BEGIN parse options>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #
while [ $# -gt Ø ]
do
   case $1 in
     -??*)                           # bundled options
       opts=$1
       while [ $opts ]
       do
          opts=`/bin/expr $opts : '.\(.*\)'`
          case $opts in
            a*)                 # ascii mode
               mode=asci
               ;;

            b*)                 # binary mode
               mode=binary
               ;;

            f*)                 # flatten remote hierarchy
               flat=true
               ;;

            g*)                 # get
               ;;

            l*)                 # list
               list=true
               ;;

            p)
               passflg=true
               shift
               if [ $# -eq Ø ]
               then
                 echo >&2 "$MYNAME: error: nopasswd"
                 echo >&2 "usage"
                 exit 1
            fi
            passwd=$1
            ;;
          p*)
             echo >&2 "$MYNAME: error: p:must be last elemenT
             of bundled options"
             exit 1
             ;;

          [rR]*)              # recursive
             recurse=true
             ;;

          ?*)
             echo >&2 "$MYNAME: error:-`/bin/expr $opts :
             '\(.\)'`: unknown option."
             echo >&2 "$usage"
             exit 1
             ;;
         esac
      done
      shift
      ;;

    -a)                             # ascii mode
       mode=ascii
       shift
       ;;

    -b)                             # binary mode
       mode=binary
       shift
       ;;

    -f)                             # flatten remote hierarchy
       flat=true
       shift
       ;;

    -g)                             # get
       shift
       ;;

    -h)                             # help
       /bin/awk '
          /^$/ {
            exit;
          }

          /^[# :]/ && NR > 1 {
            print substr ($Ø, 3);
          }

       ' $Ø |
       /bin/sed "
          /^$/{
            N
            /^\n$/D
          }
          s/\$MYNAME/$MYNAME/
       quot;
       exit Ø
       ;;

     -l)                             # list
        list=true
        shift
        ;;

     -m)                             # mirror remote hierarchy
        mirror=true
        shift
        ;;

     -p)
        passflg=true
        shift
        if [ $# -eq Ø ]
        then
           echo >&2 "$MYNAME: error: nopasswd"
           echo >&2 "usage"
           exit 1
        fi
        passwd=$1
        shift
        ;;

     -[rR])                          # recursive
        recurse=true
        shift
        ;;

      --)                             # end of options
        shift
        break
        ;;

      -*)
        echo >&2 "$MYNAME: error: $1: unknownoption."
        echo >&2 "$usage"
        exit 1
        ;;

      *)                              # end of options
        break
        ;;
    esac
   done

# << END parse options<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN parse arguments>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

case $# in
 Ø)
  list=true
  recurse=true
  ;;

 1)
   case $1 in
     *:)
       list=true
       recurse=true
       ;;
   esac
   ;;

 *)
   echo >&2 "$MYNAME: error: unexpectedargument(s)."
   echo >&2 "$usage"
   exit 1
   ;;
esac

# << END parse arguments<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN initialization>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

[ umask -ge 700 ] && umask `/bin/expr \`umask\` -0700`

# return unrooted dirname
dirname () {
   expr \
   './'${1-.}'/' : '\(\.\)/[^/]*/$' \
   \| ${1-.}'/' : '/*\(.*[^/]\)//*[^/][^/]*//*$' \
   \| .
}
[ ${1-.} = / ] && set .
host=`/bin/expr#92;
  "$1" : '[^@]*@\(.*\):' \
  \| "$1" : '\(.*\):' \
  \| \`/bin/basename\\\`pwd\\\`\`
`

path=`/bin/expr "$1" : '[^:]*:\(.*\)' \| $1.: '[^:]*:\(.*\)' \| ${1-.}`
dir=`dirname $path`
file=`/bin/basename $path`
login=`/bin/expr "$1" : '\([^@]*\)@' \|anonymous`
if /bin/ypmatch $host hosts.byname 2>&- 1>&- ||
   /bin/ypmatch $host hosts.byaddr 2>&- 1>&-
then
  port=21
  passwd=${passwd-USER@`hostname`}
else
  port=4666
  passwd=${passwd-USER@Sun.COM}
  machine=$host
  host=sun-barr.EBay
fi
if [ $list ]
then
  cmd="ls -l${recurse+R}"
  file=ls-l${recurse+R}`expr \
     $path : '\(\.\)$' \
     \| $dir$file : '\(\..*\)' \
     \| /$dir/$file | tr / .
  `
  dir=$path
else
  cmd=${recurse+m}get
  [ $recurse ] && dir=$path && file=\*
fi

case $1 in
  *@*:*)
   ;;
  *)
    [ $passflg ] ||
    eval `
      /bin/awk '
         /machine *'"${machine-$host}"'/ {
           for (i = 1; i <= NF; i++) {
             if ($i == "login") print"login="$++i";";
             if ($i == "password") print"passwd="$++i";";
           }
           exit;
         }
      ' $HOME/.netrc 2>&-
     `
     ;;
esac
# << END initialization<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #
# >> BEGIN verify prerequisites>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #
if [ ! "$flat" ]
then
  /bin/mkdir -p $dir 2>&-
  cd $dir
fi
# << END verify prerequisites<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #
# >> BEGIN main>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #
/usr/ucb/ftp -i -n -v $host $port << EOFTP
  user $login${machine+@$machine} $passwd
  ${mode-binary}
  cd $dir
  $cmd $file
  quit
EOFTP
echo ""
# << END main<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #


Previous Table of Contents Next