From f9e21a1c58cd6ec79089571650a1f60d5f9317ad Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 26 Jan 2011 13:27:10 -0800 Subject: [PATCH] archive_patch.sh: some improvements Signed-off-by: Colin McCabe --- archive_patch.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 56 insertions(+), 2 deletions(-) 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 -- 1.6.6.rc1.39.g9a42