gss_file 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #! /bin/sh
  2. # -------------------------------------------------------------
  3. # --- init
  4. # -------------------------------------------------------------
  5. # leave on error
  6. set -e
  7. # set program name and location:
  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=`basename ${script}`
  15. # -------------------------------------------------------------
  16. # --- help
  17. # -------------------------------------------------------------
  18. DisplayUsage ()
  19. {
  20. ${PAGER:-less} << EOF
  21. NAME
  22. $prog - General Storage System : files
  23. USAGE
  24. ${prog} ls [-l,--long] <filespec>
  25. -l, --long : long listing including modes, times, sizes etc
  26. ${prog} get [-f,--force] [-m,--mkdir] <source-filespec> <dest-filespec>
  27. ${prog} put [-f,--force] [-m,--mkdir] <source-filespec> <dest-filespec>
  28. -f, --force : overwrite existing destination files if necessary
  29. -m, --mkdir : create destination directories if necessary
  30. ${prog} link [-f,--force] [-n,--new] <source-filespec> <link-filespec>
  31. ${prog} unlink <source-filespec> <link-filespec>
  32. -f, --force : overwrite existing destination files if necessary
  33. -n, --new : create new empty file if necessary
  34. FILE SPECIFICATION
  35. A file specification follows the standard rules:
  36. aa/bb/c.d
  37. GENERAL OPTIONS
  38. -v, --verbose : display info on progress
  39. EOF
  40. exit 0
  41. }
  42. # err 'help text'
  43. err ()
  44. {
  45. echo "$1" 1>&2
  46. }
  47. # errit <exit-status>
  48. errit ()
  49. {
  50. err "$prog - ERROR - from : ${call}"
  51. err "$prog - ERROR - Use '$prog --help' for more info."
  52. exit $1
  53. }
  54. # -------------------------------------------------------------
  55. # --- arguments
  56. # -------------------------------------------------------------
  57. options=''
  58. task=''
  59. filespecs=''
  60. verbose=''
  61. # extract arguments
  62. for arg in "$@" ; do
  63. case ${arg} in
  64. -h | --help ) DisplayUsage ;;
  65. -v | --verbose ) verbose='T' ;;
  66. -* ) options="${options} ${arg}" ;;
  67. * ) # task or (one of the) file(s)
  68. if [ -z "${task}" ]; then
  69. task=${arg}
  70. else
  71. filespec=${arg}
  72. filespecs="${filespecs} ${filespec}"
  73. fi
  74. ;;
  75. esac
  76. done
  77. # not complete ?
  78. if [ -z "${task}" ]; then
  79. DisplayUsage
  80. exit 1
  81. fi
  82. # -------------------------------------------------------------
  83. # --- begin
  84. # -------------------------------------------------------------
  85. test ${verbose} && echo "$prog - $prog $*"
  86. # test task:
  87. case ${task} in
  88. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. # list file or directory
  90. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. 'list' )
  92. # extract options
  93. ls_options=''
  94. for option in ${options} ; do
  95. case ${option} in
  96. -l | --long ) ls_options="${ls_options} -l" ;;
  97. -* )
  98. err "$prog - ERROR - unknown option '${option}' for task '${task}'"
  99. errit 1
  100. ;;
  101. esac
  102. done
  103. # list files:
  104. /bin/ls ${ls_options} ${filespec}
  105. ;;
  106. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. # copy files
  108. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  109. 'get' | 'put' )
  110. # extract options
  111. cp_options=''
  112. do_mkdir=''
  113. for option in ${options} ; do
  114. case ${option} in
  115. -f | --force ) cp_options="${cp_options} -f" ;;
  116. -m | --mkdir ) do_mkdir='true' ;;
  117. -* )
  118. err "$prog - ERROR - unknown option '${option}' for task '${task}'"
  119. errit 1
  120. ;;
  121. esac
  122. done
  123. # extract source and destination filespec:
  124. source_spec=''
  125. dest_spec=''
  126. for spec in ${filespecs} ; do
  127. if [ -z "${source_spec}" ]; then
  128. source_spec=${spec}
  129. elif [ -z "${dest_spec}" ]; then
  130. dest_spec=${spec}
  131. else
  132. err "$prog - ERROR - tasks 'get' and 'put' require source and destination file specifications only."
  133. errit 1
  134. fi
  135. done
  136. if [ -z "${dest_spec}" ]; then
  137. err "$prog - ERROR - tasks 'get' and 'put' require source and destination file specifications."
  138. errit 1
  139. fi
  140. # create destination directory ?
  141. # o destination is an existing file : not necessary
  142. # o destination is an existing directory : not necessary
  143. if [ ${do_mkdir} ] && [ ! -f ${dest_spec} ] && [ ! -d ${dest_spec} ]; then
  144. # destination is not an existing file or directory;
  145. # create the directory part of the destination:
  146. dest_dir=`/usr/bin/dirname ${dest_spec}`
  147. test "${dest_dir}" != "." && /bin/mkdir -p ${dest_dir}
  148. fi
  149. # copy file; suppress error messages:
  150. /bin/cp ${cp_options} ${source_spec} ${dest_spec} > /dev/null 2>&1
  151. ;;
  152. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. # task ???
  154. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  155. * )
  156. err "$prog - ERROR - unsupported task '${task}'"
  157. errit 1
  158. ;;
  159. esac # tasks
  160. # -------------------------------------------------------------
  161. # --- end
  162. # -------------------------------------------------------------
  163. test ${verbose} && echo "$prog - end"
  164. # ok
  165. exit 0