gss_ftp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #! /bin/sh
  2. # -------------------------------------------------------------
  3. # --- init
  4. # -------------------------------------------------------------
  5. # leave on error
  6. set -e
  7. # set program name and filespec:
  8. call="$0 $*"
  9. case $0 in
  10. /* ) script=$0 ;;
  11. * ) script="`/bin/pwd`/$0" ;;
  12. esac
  13. bindir=`/usr/bin/dirname ${script}`
  14. prog=`/bin/basename ${script}`
  15. # -------------------------------------------------------------
  16. # --- help
  17. # -------------------------------------------------------------
  18. DisplayUsage ()
  19. {
  20. ${PAGER:-less} << EOF
  21. NAME
  22. $prog - General Storage System : ECMWF EcAccess gateway
  23. USAGE
  24. ${prog} list [-l,--long] <filespec>
  25. -l, --long : long listing including modes, times, sizes etc
  26. ${prog} get [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
  27. ${prog} put [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
  28. -f, --force : overwrite existing destination files if necessary
  29. -m, --mkdir : create destination directories if necessary
  30. FILE SPECIFICATION
  31. A file is specified by the name of the ftp server, double dot,
  32. and standard unix file name:
  33. ftp.int:path/file
  34. Use '~/.netrc' to assign login names and passwords to the ftp address:
  35. machine ftp.int login anonymous password you@work.com
  36. GENERAL OPTIONS
  37. -v, --verbose : display info on progress
  38. EOF
  39. exit 0
  40. }
  41. # err 'help text'
  42. err ()
  43. {
  44. echo "$1" 1>&2
  45. }
  46. # errit <exit-status>
  47. errit ()
  48. {
  49. err "$prog - ERROR - from : ${call}"
  50. err "$prog - ERROR - Use '$prog --help' for more info."
  51. exit $1
  52. }
  53. # -------------------------------------------------------------
  54. # --- arguments
  55. # -------------------------------------------------------------
  56. options=''
  57. task=''
  58. filespecs=''
  59. verbose=''
  60. # extract arguments
  61. for arg in "$@" ; do
  62. case ${arg} in
  63. -h | --help ) DisplayUsage ;;
  64. -v | --verbose ) verbose='T' ;;
  65. -* ) options="${options} ${arg}" ;;
  66. * ) # task or (one of the) file(s)
  67. if [ -z "${task}" ]; then
  68. task=${arg}
  69. else
  70. filespec=${arg}
  71. filespecs="${filespecs} ${filespec}"
  72. fi
  73. ;;
  74. esac
  75. done
  76. # not complete ?
  77. if [ -z "${task}" ]; then
  78. err "$prog - no arguments specified"
  79. errit 1
  80. fi
  81. # -------------------------------------------------------------
  82. # --- functions
  83. # -------------------------------------------------------------
  84. #
  85. # EvalSpecials ftp.int:data/a.txt
  86. # host=${FTP_HOST}
  87. # filespec=${FS}
  88. #
  89. # Extract the ftp host name, store in 'FTP_HOST',
  90. # return last part in variable 'FS' .
  91. #
  92. EvalSpecials ()
  93. {
  94. # split in ftp host name and rest:
  95. FTP_HOST="`echo $1 | /usr/bin/cut -d ':' -f 1`"
  96. FS="`echo $1 | /usr/bin/cut -d ':' -f 2`"
  97. }
  98. # -------------------------------------------------------------
  99. # --- begin
  100. # -------------------------------------------------------------
  101. test ${verbose} && echo "$prog - $0 $*"
  102. # test task:
  103. case ${task} in
  104. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. # list file or directory
  106. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. 'list' )
  108. # extract options
  109. #eccommand='ecls'
  110. for option in ${options} ; do
  111. case ${option} in
  112. #-l | --long ) eccommand="ecdir" ;;
  113. -* )
  114. err "$prog - unknown option '${option}' for task '${task}'"
  115. errit 1
  116. ;;
  117. esac
  118. done
  119. # extract source and destination filespec:
  120. filespec=''
  121. for spec in ${filespecs} ; do
  122. EvalSpecials ${spec}
  123. if [ -z "${filespec}" ]; then
  124. filespec=${FS}
  125. else
  126. err "$prog - ERROR - task '${task}' requires single file specification only."
  127. errit 1
  128. fi
  129. done
  130. if [ -z "${filespec}" ]; then
  131. err "$prog - ERROR - tasks '${task}' requires file specification."
  132. errit 1
  133. fi
  134. # list files:
  135. /usr/bin/ftp ${FTP_HOST} << EOF
  136. dir ${filespec}
  137. EOF
  138. ;;
  139. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. # copy files
  141. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  142. 'get' )
  143. # extract options
  144. ecget_options=''
  145. do_mkdir=''
  146. for option in ${options} ; do
  147. case ${option} in
  148. -f | --force ) ecget_options="${ecget_options}" ;;
  149. -m | --mkdir ) do_mkdir='true' ;;
  150. -* )
  151. err "$prog - unknown option '${option}' for task '${task}'"
  152. errit 1
  153. ;;
  154. esac
  155. done
  156. # extract source and destination filespec:
  157. source_spec=''
  158. dest_spec=''
  159. for spec in ${filespecs} ; do
  160. EvalSpecials ${spec}
  161. if [ -z "${source_spec}" ]; then
  162. source_spec=${FS}
  163. elif [ -z "${dest_spec}" ]; then
  164. dest_spec=${FS}
  165. else
  166. err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
  167. errit 1
  168. fi
  169. done
  170. if [ -z "${dest_spec}" ]; then
  171. err "$prog - ERROR - task '${task}' requires source and destination file specifications."
  172. errit 1
  173. fi
  174. # create destination directory ?
  175. # o destination is an existing file : not necessary
  176. # o destination is an existing directory : not necessary
  177. if [ ${do_mkdir} ] && [ ! -f ${dest_spec} ] && [ ! -d ${dest_spec} ]; then
  178. # destination is not an existing file or directory;
  179. # create the directory part of the destination:
  180. dest_dir=`/usr/bin/dirname ${dest_spec}`
  181. test "${dest_dir}" != "." && /bin/mkdir -p ${dest_dir}
  182. fi
  183. # copy file; suppress error messages:
  184. /usr/bin/ftp ${FTP_HOST} << EOF
  185. get ${source_spec} ${dest_spec}
  186. EOF
  187. ;;
  188. 'put' )
  189. # extract options
  190. ecput_options=''
  191. do_mkdir=''
  192. for option in ${options} ; do
  193. case ${option} in
  194. -f | --force ) ecput_options="${ecput_options}" ;;
  195. -m | --mkdir ) do_mkdir='true' ;;
  196. -* )
  197. err "$prog - unknown option '${option}' for task '${task}'"
  198. errit 1
  199. ;;
  200. esac
  201. done
  202. # extract source and destination filespec:
  203. source_spec=''
  204. dest_spec=''
  205. for spec in ${filespecs} ; do
  206. EvalSpecials ${spec}
  207. if [ -z "${source_spec}" ]; then
  208. source_spec=${FS}
  209. elif [ -z "${dest_spec}" ]; then
  210. dest_spec=${FS}
  211. else
  212. err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
  213. errit 1
  214. fi
  215. done
  216. if [ -z "${dest_spec}" ]; then
  217. err "$prog - ERROR - task '${task}' requires source and destination file specifications."
  218. errit 1
  219. fi
  220. # create destination directory ?
  221. mkdir_command=''
  222. if [ ${do_mkdir} ]; then
  223. # o destination is an existing file : not necessary
  224. # o destination is an existing directory : not necessary
  225. #if ! ${script} exist ${dest_spec} ; then
  226. # do not test if directory already exists,
  227. # this takes more time than just creation ..
  228. # supress error messages about already existing directories ...
  229. dest_dir=`/usr/bin/dirname ${dest_spec}`
  230. mkdir_command="mkdir ${dest_dir}"
  231. #fi
  232. fi
  233. # copy file:
  234. outfile='.gss.out'
  235. /usr/bin/ftp ${FTP_HOST} > ${outfile} 2>&1 << EOF
  236. ${mkdir_command}
  237. put ${source_spec} ${dest_spec}
  238. EOF
  239. # check on error :
  240. if ( /bin/grep 'Could not create file.' ${outfile} > /dev/null ) ; then
  241. err "$prog - ERROR - from ftp:"
  242. err "$prog - -------------------------------------------"
  243. /bin/cat ${outfile}
  244. err "$prog - -------------------------------------------"
  245. errit 1
  246. else
  247. # ok
  248. /bin/rm -f ${outfile}
  249. fi
  250. ;;
  251. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  252. # task ???
  253. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  254. * )
  255. err "$prog - ERROR - unsupported task '${task}'"
  256. errit 1
  257. ;;
  258. esac # tasks
  259. # -------------------------------------------------------------
  260. # --- end
  261. # -------------------------------------------------------------
  262. test ${verbose} && echo "$prog - end"
  263. # ok
  264. exit 0