Modularised code.

--interactive long option accepts arguments.

Fixed bug with trashing file that cannot be found.
This commit is contained in:
Robert Rothenberg 2011-03-17 14:46:49 +00:00
parent 144d10da69
commit 98f88cf1dc

45
trash
View File

@ -15,7 +15,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
version="0.3.2 \$Id: trash 2011/03/17 14:11:32 GMT rr@dwaible $" version="0.3.3 \$Id: trash 2011/03/17 14:46:39 GMT rr@dwaible $"
function show_usage { function show_usage {
cat << EOU cat << EOU
@ -30,16 +30,20 @@ Options:
EOU EOU
} }
function try_help {
echo "Try \`$0 --help' for more information." 1>&2
exit 1
}
# Option handling # Option handling
if ! options=$(getopt -o hvi -l help,verbose,interactive,version -- "$@") if ! options=$(getopt -o hvi -l "help,verbose,interactive::,version" -n "$0" -- $@)
then then
exit 1 exit 1
fi fi
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
echo "Try \`$0 --help' for more information." 1>&2 try_help
exit 1
fi fi
set -- $options set -- $options
@ -47,15 +51,29 @@ set -- $options
verbose=0 verbose=0
interactive=never interactive=never
function strip_quotes {
x=$1
x=${x#\'}
x=${x%\'}
echo "${x}"
}
while [ $# -gt 0 ] while [ $# -gt 0 ]
do do
case $1 in case $1 in
-h|--help) show_usage ; exit 1;; -h|--help) show_usage ; exit 1;;
-v|--verbose) verbose=1 ;; -v|--verbose) verbose=1 ;;
-i|--interactive) interactive=always ;; -i) interactive=always ;;
--interactive) shift ;
arg=`strip_quotes "$1"`
if [ -z "$arg" ]; then
arg=always
fi
interactive=$arg
;;
--version) echo "$0 $version" 1>&2;; --version) echo "$0 $version" 1>&2;;
(--) shift; break;; (--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;; (-*) echo "$0: invalid option -- '$1'" 1>&2 ; try_help ; exit 1 ;;
(*) break;; (*) break;;
esac esac
shift shift
@ -150,6 +168,12 @@ fi
function can_trash { function can_trash {
filename=$1 filename=$1
if [ ! -e "$filename" ]; then
echo "$0: cannot move \`$filename' to trash: No such file or directory" 1>&2
echo 0
else
type="file" type="file"
if [ -d "$filename" ]; then if [ -d "$filename" ]; then
type="directory" type="directory"
@ -167,6 +191,7 @@ function can_trash {
;; ;;
*) echo "$0: unsupported value interactive=${interactive}" 1>&2 ; echo 0; exit 1 ;; *) echo "$0: unsupported value interactive=${interactive}" 1>&2 ; echo 0; exit 1 ;;
esac esac
fi
} }
function init_trashdir { function init_trashdir {
@ -174,13 +199,13 @@ function init_trashdir {
mkdir -p "$trashdir/files" mkdir -p "$trashdir/files"
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
echo "Unable to write to $trashdir" 1>&2 echo "$0: unable to write to $trashdir" 1>&2
exit 2 exit 2
fi fi
mkdir -p "$trashdir/info" mkdir -p "$trashdir/info"
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
echo "Unable to write to $trashdir" 1>&2 echo "$0: unable to write to $trashdir" 1>&2
exit 2 exit 2
fi fi
} }
@ -221,12 +246,12 @@ END
mv $mv_opts "$filename" "$deletedfile" mv $mv_opts "$filename" "$deletedfile"
} }
for f in "$@" for f in "$@"
do do
# strip quotes added by getopts # strip quotes added by getopts
f=${f#\'} f=`strip_quotes "${f}"`
f=${f%\'}
# get full pathname of file # get full pathname of file