From: Colin Patrick McCabe Date: Wed, 26 Jan 2011 21:27:10 +0000 (-0800) Subject: archive_patch.sh: some improvements X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9e21a1c58cd6ec79089571650a1f60d5f9317ad;p=cmccabe-bin archive_patch.sh: some improvements Signed-off-by: Colin McCabe --- diff --git a/archive_patch.sh b/archive_patch.sh index 8c7a236..082f698 100755 --- a/archive_patch.sh +++ b/archive_patch.sh @@ -1,5 +1,59 @@ #!/bin/sh -new_file_name=`date +'%Y-%m-%d'`_`basename $1` +# +# archive_patch.sh +# +# Archives old files +# +# copyright Colin Patrick McCabe, 2011 +# -mv -i "${1}" "${new_file_name}" +die() { + echo $@ + exit 1 +} + +usage() { + echo + echo "$0: archives old files" + echo + echo "-c copy the file rather than moving it" + echo "-d directory where the file should end up (default: .)" + echo "-h This help message" + echo +} + +copy=0 +destdir=. +while getopts "cd:h" flag +do + case $flag in + c) copy=1;; + + d) destdir=$OPTARG;; + + h) usage + exit 1 + ;; + + *) usage + exit 1;; + esac +done +shift $((OPTIND-1)) + +if [ $copy -eq 1 ]; then + cmd="cp -f" +else + cmd="mv -f" +fi + +[ -d "$destdir" ] || die "destdir '$destdir' is not a directory" + +for file in "$@"; do + [ -e "$file" ] || die "'$file' does not exist" +done +for file in "$@"; do + new_file_name=$destdir/`date +'%Y-%m-%d'`_`basename $file` + $cmd "${file}" "${new_file_name}" || die "failed on $file" +done