123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- #! /bin/sh
- # -------------------------------------------------------------
- # --- init
- # -------------------------------------------------------------
- # leave on error
- set -e
- # set program name and filespec:
- call="$0 $*"
- case $0 in
- /* ) script=$0 ;;
- * ) script="`/bin/pwd`/$0" ;;
- esac
- bindir=`/usr/bin/dirname ${script}`
- prog=`/bin/basename ${script}`
- # -------------------------------------------------------------
- # --- help
- # -------------------------------------------------------------
- DisplayUsage ()
- {
- ${PAGER:-less} << EOF
- NAME
- $prog - General Storage System : ECMWF EcAccess gateway
-
- USAGE
- ${prog} list [-l,--long] <filespec>
-
- -l, --long : long listing including modes, times, sizes etc
- ${prog} get [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
- ${prog} put [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
-
- -f, --force : overwrite existing destination files if necessary
- -m, --mkdir : create destination directories if necessary
-
- FILE SPECIFICATION
- A file is specified by the name of the ftp server, double dot,
- and standard unix file name:
- ftp.int:path/file
- Use '~/.netrc' to assign login names and passwords to the ftp address:
-
- machine ftp.int login anonymous password you@work.com
- GENERAL OPTIONS
- -v, --verbose : display info on progress
- EOF
- exit 0
- }
- # err 'help text'
- err ()
- {
- echo "$1" 1>&2
- }
- # errit <exit-status>
- errit ()
- {
- err "$prog - ERROR - from : ${call}"
- err "$prog - ERROR - Use '$prog --help' for more info."
- exit $1
- }
- # -------------------------------------------------------------
- # --- arguments
- # -------------------------------------------------------------
- options=''
- task=''
- filespecs=''
- verbose=''
- # extract arguments
- for arg in "$@" ; do
- case ${arg} in
- -h | --help ) DisplayUsage ;;
- -v | --verbose ) verbose='T' ;;
- -* ) options="${options} ${arg}" ;;
- * ) # task or (one of the) file(s)
- if [ -z "${task}" ]; then
- task=${arg}
- else
- filespec=${arg}
- filespecs="${filespecs} ${filespec}"
- fi
- ;;
- esac
- done
- # not complete ?
- if [ -z "${task}" ]; then
- err "$prog - no arguments specified"
- errit 1
- fi
- # -------------------------------------------------------------
- # --- functions
- # -------------------------------------------------------------
- #
- # EvalSpecials ftp.int:data/a.txt
- # host=${FTP_HOST}
- # filespec=${FS}
- #
- # Extract the ftp host name, store in 'FTP_HOST',
- # return last part in variable 'FS' .
- #
- EvalSpecials ()
- {
- # split in ftp host name and rest:
- FTP_HOST="`echo $1 | /usr/bin/cut -d ':' -f 1`"
- FS="`echo $1 | /usr/bin/cut -d ':' -f 2`"
- }
- # -------------------------------------------------------------
- # --- begin
- # -------------------------------------------------------------
- test ${verbose} && echo "$prog - $0 $*"
- # test task:
- case ${task} in
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # list file or directory
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'list' )
- # extract options
- #eccommand='ecls'
- for option in ${options} ; do
- case ${option} in
- #-l | --long ) eccommand="ecdir" ;;
- -* )
- err "$prog - unknown option '${option}' for task '${task}'"
- errit 1
- ;;
- esac
- done
-
- # extract source and destination filespec:
- filespec=''
- for spec in ${filespecs} ; do
- EvalSpecials ${spec}
- if [ -z "${filespec}" ]; then
- filespec=${FS}
- else
- err "$prog - ERROR - task '${task}' requires single file specification only."
- errit 1
- fi
- done
- if [ -z "${filespec}" ]; then
- err "$prog - ERROR - tasks '${task}' requires file specification."
- errit 1
- fi
- # list files:
- /usr/bin/ftp ${FTP_HOST} << EOF
- dir ${filespec}
- EOF
- ;;
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # copy files
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'get' )
- # extract options
- ecget_options=''
- do_mkdir=''
- for option in ${options} ; do
- case ${option} in
- -f | --force ) ecget_options="${ecget_options}" ;;
- -m | --mkdir ) do_mkdir='true' ;;
- -* )
- err "$prog - unknown option '${option}' for task '${task}'"
- errit 1
- ;;
- esac
- done
- # extract source and destination filespec:
- source_spec=''
- dest_spec=''
- for spec in ${filespecs} ; do
- EvalSpecials ${spec}
- if [ -z "${source_spec}" ]; then
- source_spec=${FS}
- elif [ -z "${dest_spec}" ]; then
- dest_spec=${FS}
- else
- err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
- errit 1
- fi
- done
- if [ -z "${dest_spec}" ]; then
- err "$prog - ERROR - task '${task}' requires source and destination file specifications."
- errit 1
- fi
-
- # create destination directory ?
- # o destination is an existing file : not necessary
- # o destination is an existing directory : not necessary
- if [ ${do_mkdir} ] && [ ! -f ${dest_spec} ] && [ ! -d ${dest_spec} ]; then
- # destination is not an existing file or directory;
- # create the directory part of the destination:
- dest_dir=`/usr/bin/dirname ${dest_spec}`
- test "${dest_dir}" != "." && /bin/mkdir -p ${dest_dir}
- fi
-
- # copy file; suppress error messages:
- /usr/bin/ftp ${FTP_HOST} << EOF
- get ${source_spec} ${dest_spec}
- EOF
-
- ;;
- 'put' )
- # extract options
- ecput_options=''
- do_mkdir=''
- for option in ${options} ; do
- case ${option} in
- -f | --force ) ecput_options="${ecput_options}" ;;
- -m | --mkdir ) do_mkdir='true' ;;
- -* )
- err "$prog - unknown option '${option}' for task '${task}'"
- errit 1
- ;;
- esac
- done
- # extract source and destination filespec:
- source_spec=''
- dest_spec=''
- for spec in ${filespecs} ; do
- EvalSpecials ${spec}
- if [ -z "${source_spec}" ]; then
- source_spec=${FS}
- elif [ -z "${dest_spec}" ]; then
- dest_spec=${FS}
- else
- err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
- errit 1
- fi
- done
- if [ -z "${dest_spec}" ]; then
- err "$prog - ERROR - task '${task}' requires source and destination file specifications."
- errit 1
- fi
-
- # create destination directory ?
- mkdir_command=''
- if [ ${do_mkdir} ]; then
- # o destination is an existing file : not necessary
- # o destination is an existing directory : not necessary
- #if ! ${script} exist ${dest_spec} ; then
- # do not test if directory already exists,
- # this takes more time than just creation ..
- # supress error messages about already existing directories ...
- dest_dir=`/usr/bin/dirname ${dest_spec}`
- mkdir_command="mkdir ${dest_dir}"
- #fi
- fi
- # copy file:
- outfile='.gss.out'
- /usr/bin/ftp ${FTP_HOST} > ${outfile} 2>&1 << EOF
- ${mkdir_command}
- put ${source_spec} ${dest_spec}
- EOF
- # check on error :
- if ( /bin/grep 'Could not create file.' ${outfile} > /dev/null ) ; then
- err "$prog - ERROR - from ftp:"
- err "$prog - -------------------------------------------"
- /bin/cat ${outfile}
- err "$prog - -------------------------------------------"
- errit 1
- else
- # ok
- /bin/rm -f ${outfile}
- fi
-
- ;;
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # task ???
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * )
- err "$prog - ERROR - unsupported task '${task}'"
- errit 1
- ;;
-
- esac # tasks
- # -------------------------------------------------------------
- # --- end
- # -------------------------------------------------------------
- test ${verbose} && echo "$prog - end"
- # ok
- exit 0
|