| 1 | #! /bin/sh |
| 2 | # $Id: ftpget,v 1.2 91/05/07 21:15:43 moraes Exp $ |
| 3 | # Script to perform batch anonymous ftp. Essentially converts a list of |
| 4 | # of command-line arguments into input to ftp. |
| 5 | # ==> This script is nothing but a shell wrapper around "ftp" . . . |
| 6 | # Simple, and quick - written as a companion to ftplist |
| 7 | # -h specifies the remote host (default prep.ai.mit.edu) |
| 8 | # -d specifies the remote directory to cd to - you can provide a sequence |
| 9 | # of -d options - they will be cd'ed to in turn. If the paths are relative, |
| 10 | # make sure you get the sequence right. Be careful with relative paths - |
| 11 | # there are far too many symlinks nowadays. |
| 12 | # (default is the ftp login directory) |
| 13 | # -v turns on the verbose option of ftp, and shows all responses from the |
| 14 | # ftp server. |
| 15 | # -f remotefile[:localfile] gets the remote file into localfile |
| 16 | # -m pattern does an mget with the specified pattern. Remember to quote |
| 17 | # shell characters. |
| 18 | # -c does a local cd to the specified directory |
| 19 | # For example, |
| 20 | # ftpget -h expo.lcs.mit.edu -d contrib -f xplaces.shar:xplaces.sh \ |
| 21 | # -d ../pub/R3/fixes -c ~/fixes -m 'fix*' |
| 22 | # will get xplaces.shar from ~ftp/contrib on expo.lcs.mit.edu, and put it |
| 23 | # in xplaces.sh in the current working directory, and get all fixes from |
| 24 | # ~ftp/pub/R3/fixes and put them in the ~/fixes directory. |
| 25 | # Obviously, the sequence of the options is important, since the equivalent |
| 26 | # commands are executed by ftp in corresponding order |
| 27 | # |
| 28 | # Mark Moraes <[email protected]>, Feb 1, 1989 |
| 29 | # |
| 30 | |
| 31 | |
| 32 | # ==> These comments added by author of this document. |
| 33 | |
| 34 | # PATH=/local/bin:/usr/ucb:/usr/bin:/bin |
| 35 | # export PATH |
| 36 | # ==> Above 2 lines from original script probably superfluous. |
| 37 | |
| 38 | E_BADARGS=65 |
| 39 | |
| 40 | TMPFILE=/tmp/ftp.$$ |
| 41 | # ==> Creates temp file, using process id of script ($$) |
| 42 | # ==> to construct filename. |
| 43 | |
| 44 | SITE=`domainname`.toronto.edu |
| 45 | # ==> 'domainname' similar to 'hostname' |
| 46 | # ==> May rewrite this to parameterize this for general use. |
| 47 | |
| 48 | usage="Usage: $0 [-h remotehost] [-d remotedirectory]... \ |
| 49 | [-f remfile:localfile]... [-c localdirectory] [-m filepattern] [-v]" |
| 50 | ftpflags="-i -n" |
| 51 | verbflag= |
| 52 | set -f # So we can use globbing in -m |
| 53 | set x `getopt vh:d:c:m:f: $*` |
| 54 | if [ $? != 0 ]; then |
| 55 | echo $usage |
| 56 | exit $E_BADARGS |
| 57 | fi |
| 58 | shift |
| 59 | trap 'rm -f ${TMPFILE} ; exit' 0 1 2 3 15 |
| 60 | # ==> Signals: HUP INT (Ctl-C) QUIT TERM |
| 61 | # ==> Delete tempfile in case of abnormal exit from script. |
| 62 | echo "user anonymous ${USER-gnu}@${SITE} > ${TMPFILE}" |
| 63 | # ==> Added quotes (recommended in complex echoes). |
| 64 | echo binary >> ${TMPFILE} |
| 65 | for i in $* # ==> Parse command-line args. |
| 66 | do |
| 67 | case $i in |
| 68 | -v) verbflag=-v; echo hash >> ${TMPFILE}; shift;; |
| 69 | -h) remhost=$2; shift 2;; |
| 70 | -d) echo cd $2 >> ${TMPFILE}; |
| 71 | if [ x${verbflag} != x ]; then |
| 72 | echo pwd >> ${TMPFILE}; |
| 73 | fi; |
| 74 | shift 2;; |
| 75 | -c) echo lcd $2 >> ${TMPFILE}; shift 2;; |
| 76 | -m) echo mget "$2" >> ${TMPFILE}; shift 2;; |
| 77 | -f) f1=`expr "$2" : "\([^:]*\).*"`; f2=`expr "$2" : "[^:]*:\(.*\)"`; |
| 78 | echo get ${f1} ${f2} >> ${TMPFILE}; shift 2;; |
| 79 | --) shift; break;; |
| 80 | esac |
| 81 | # ==> 'lcd' and 'mget' are ftp commands. See "man ftp" . . . |
| 82 | done |
| 83 | if [ $# -ne 0 ]; then |
| 84 | echo $usage |
| 85 | exit $E_BADARGS |
| 86 | # ==> Changed from "exit 2" to conform with style standard. |
| 87 | fi |
| 88 | if [ x${verbflag} != x ]; then |
| 89 | ftpflags="${ftpflags} -v" |
| 90 | fi |
| 91 | if [ x${remhost} = x ]; then |
| 92 | remhost=prep.ai.mit.edu |
| 93 | # ==> Change to match appropriate ftp site. |
| 94 | fi |
| 95 | echo quit >> ${TMPFILE} |
| 96 | # ==> All commands saved in tempfile. |
| 97 | |
| 98 | ftp ${ftpflags} ${remhost} < ${TMPFILE} |
| 99 | # ==> Now, tempfile batch processed by ftp. |
| 100 | |
| 101 | rm -f ${TMPFILE} |
| 102 | # ==> Finally, tempfile deleted (you may wish to copy it to a logfile). |
| 103 | |
| 104 | |
| 105 | # ==> Exercises: |
| 106 | # ==> --------- |
| 107 | # ==> 1) Add error checking. |
| 108 | # ==> 2) Add bells & whistles. |
| 109 | |
| 110 | # See the included file "Moraes-COPYRIGHT" for copyright info. |
| 111 | #+ on this script. |