#! /bin/sh # ------------------------------------------------------------- # --- init # ------------------------------------------------------------- # leave on error set -e # set program name and location: call="$0 $*" case $0 in /* ) script=$0 ;; * ) script="`/bin/pwd`/$0" ;; esac bindir=`/usr/bin/dirname ${script}` prog=`basename ${script}` # ------------------------------------------------------------- # --- help # ------------------------------------------------------------- DisplayUsage () { ${PAGER:-less} << EOF NAME $prog - General Storage System : files USAGE ${prog} ls [-l,--long] -l, --long : long listing including modes, times, sizes etc ${prog} get [-f,--force] [-m,--mkdir] ${prog} put [-f,--force] [-m,--mkdir] -f, --force : overwrite existing destination files if necessary -m, --mkdir : create destination directories if necessary ${prog} link [-f,--force] [-n,--new] ${prog} unlink -f, --force : overwrite existing destination files if necessary -n, --new : create new empty file if necessary FILE SPECIFICATION A file specification follows the standard rules: aa/bb/c.d GENERAL OPTIONS -v, --verbose : display info on progress EOF exit 0 } # err 'help text' err () { echo "$1" 1>&2 } # errit 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 DisplayUsage exit 1 fi # ------------------------------------------------------------- # --- begin # ------------------------------------------------------------- test ${verbose} && echo "$prog - $prog $*" # test task: case ${task} in # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # list file or directory # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'list' ) # extract options ls_options='' for option in ${options} ; do case ${option} in -l | --long ) ls_options="${ls_options} -l" ;; -* ) err "$prog - ERROR - unknown option '${option}' for task '${task}'" errit 1 ;; esac done # list files: /bin/ls ${ls_options} ${filespec} ;; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # copy files # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'get' | 'put' ) # extract options cp_options='' do_mkdir='' for option in ${options} ; do case ${option} in -f | --force ) cp_options="${cp_options} -f" ;; -m | --mkdir ) do_mkdir='true' ;; -* ) err "$prog - ERROR - unknown option '${option}' for task '${task}'" errit 1 ;; esac done # extract source and destination filespec: source_spec='' dest_spec='' for spec in ${filespecs} ; do if [ -z "${source_spec}" ]; then source_spec=${spec} elif [ -z "${dest_spec}" ]; then dest_spec=${spec} else err "$prog - ERROR - tasks 'get' and 'put' require source and destination file specifications only." errit 1 fi done if [ -z "${dest_spec}" ]; then err "$prog - ERROR - tasks 'get' and 'put' require 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: /bin/cp ${cp_options} ${source_spec} ${dest_spec} > /dev/null 2>&1 ;; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # task ??? # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ) err "$prog - ERROR - unsupported task '${task}'" errit 1 ;; esac # tasks # ------------------------------------------------------------- # --- end # ------------------------------------------------------------- test ${verbose} && echo "$prog - end" # ok exit 0