#!/bin/bash #IFS=$'\n' syntax() { printf "\n" printf " Synopsis:\n\n" printf " `basename $0` FILE\n" printf " `basename $0` -m FILE\n" printf " `basename $0` -h\n" printf "\n" printf " Options:\n" printf " -m : Move file rather than copying it.\n" printf " -h : Print this help screen.\n" printf "\n" } ACTION=cp TEST=0 OPTSTRING="hms:t" while [ 1 -eq 1 ] ; do getopts $OPTSTRING OPT $@ if [ $? -ne 0 ] ; then break fi case $OPT in 'h') syntax exit ;; 'm') ACTION=mv ;; 't') printf "Test mode\n" TEST=1 ;; *) ;; esac #echo OPT = \'${OPT}\', OPTIND = \'${OPTIND}\' done shift $(($OPTIND-1)) # # Don't try to handle more than one argument at a time. # If we have more than one argument, call ourself with # just one argument. # # But preserve the options (and option arguments # if we even have any)! # if [[ $# -gt 1 ]] ; then for FILE in $@; do CMD="" if [[ $TEST -eq 1 ]] ; then CMD="$CMD -t" fi if [[ $ACTION == "mv" ]] ; then CMD="$CMD -m" fi CMD="$CMD ${FILE}" $0 $CMD done exit fi ARG=$1 if [[ "_$ARG" = "_" ]]; then syntax exit fi case "_$ARG" in _|_-*) syntax exit ;; *) ;; esac if [[ ! -e $ARG ]]; then printf "No such file: \'$ARG\'\n" exit 1 fi if [ -f $ARG ] ; then TYPE=f elif [ -d $ARG ] ; then TYPE=d else printf "Wrong file type for archive: \'$ARG\'\n" exit 1 fi if [ ! -d .archive ] ; then mkdir .archive if [ $? -ne 0 ] ; then printf "Error creating directory \'.archive\'\n" exit 1 fi fi DATE_TIME=`date +%y%m%d%H%M%S` MEAT=`echo ${ARG} | awk -F\. '{print $1}'` MEAT=${MEAT/%\//} MEAT_LEN=${#MEAT} POTATOES=${ARG:${MEAT_LEN}} POTATOES=${POTATOES/%\//} #echo DATE_TIME = \'${DATE_TIME}\' #echo MEAT = \'${MEAT}\' #echo MEAT_LEN = \'${MEAT_LEN}\' #echo POTATOES = \'${POTATOES}\' #exit #if [ $MEAT_LEN -lt ${#ARG} ]; then # echo "Have Extension" #fi FILENAME=${MEAT}_${DATE_TIME}${POTATOES} #echo $FILENAME CMD="${ACTION}" if [[ $TYPE = d ]]; then if [[ $CMD == "cp" ]] ; then if [[ $TEST -ne 1 ]] ; then ${CMD} -r "$ARG" ".archive/$FILENAME" fi elif [[ $CMD == "mv" ]] ; then if [[ ! -w ${ARG} ]] ; then printf "Error: You must have write privileges on \'$ARG\' in order to archive it.\n" exit 1 fi if [[ $TEST -ne 1 ]] ; then mv "${ARG}" ".archive/" mv ".archive/${ARG}" ".archive/$FILENAME" fi else echo "Error: Unknown command: \'${ACTION}\'" fi else if [[ $TEST -ne 1 ]] ; then ${CMD} "${ARG}" ".archive/${FILENAME}" fi fi if [[ $? -ne 0 ]] ; then printf "Error archiving \'${ARG}\' to \'.archive/${FILENAME}\'\n" exit 1 fi if [[ ${TYPE} == 'd' ]]; then MOD="555" CMD="chmod -R" else MOD="444" CMD="chmod" fi if [[ $TEST -ne 1 ]] ; then ${CMD} ${MOD} .archive/${FILENAME} fi printf "\'.archive/$FILENAME\' - OK\n"