Modularised code.
Added -i,--interactive option for confirmation.
This commit is contained in:
parent
deaf3beea9
commit
144d10da69
66
trash
66
trash
@ -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.1 \$Id: trash 2011/03/17 13:34:24 GMT rr@dwaible $"
|
version="0.3.2 \$Id: trash 2011/03/17 14:11:32 GMT rr@dwaible $"
|
||||||
|
|
||||||
function show_usage {
|
function show_usage {
|
||||||
cat << EOU
|
cat << EOU
|
||||||
@ -32,7 +32,7 @@ EOU
|
|||||||
|
|
||||||
# Option handling
|
# Option handling
|
||||||
|
|
||||||
if ! options=$(getopt -o hv -l help,verbose,version -- "$@")
|
if ! options=$(getopt -o hvi -l help,verbose,interactive,version -- "$@")
|
||||||
then
|
then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@ -45,12 +45,14 @@ fi
|
|||||||
set -- $options
|
set -- $options
|
||||||
|
|
||||||
verbose=0
|
verbose=0
|
||||||
|
interactive=never
|
||||||
|
|
||||||
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 ;;
|
||||||
--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: error - unrecognized option $1" 1>&2; exit 1;;
|
||||||
@ -145,19 +147,30 @@ if [ $verbose != 0 ]; then
|
|||||||
mv_opts="${mv_opts} -v"
|
mv_opts="${mv_opts} -v"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for f in "$@"
|
function can_trash {
|
||||||
do
|
filename=$1
|
||||||
# strip quotes added by getopts
|
|
||||||
|
|
||||||
f=${f#\'}
|
type="file"
|
||||||
f=${f%\'}
|
if [ -d "$filename" ]; then
|
||||||
|
type="directory"
|
||||||
|
fi
|
||||||
|
|
||||||
# get full pathname of file
|
case $interactive in
|
||||||
|
never) echo 1 ;;
|
||||||
|
always)
|
||||||
|
read -p "$0: move ${type} '${filename}' to trash?" yn
|
||||||
|
if [[ "$yn" =~ ^[yY]$ ]]; then
|
||||||
|
echo 1
|
||||||
|
else
|
||||||
|
echo 0
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*) echo "$0:unsupported value interactive=${interactive}" 1>&2 ; echo 0; exit 1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
filename=$(readlink -f "${f}")
|
function init_trashdir {
|
||||||
dir=${filename%/*}
|
trashdir=$1
|
||||||
|
|
||||||
trashdir=`get_trashdir "$dir"`
|
|
||||||
|
|
||||||
mkdir -p "$trashdir/files"
|
mkdir -p "$trashdir/files"
|
||||||
if [ "$?" != "0" ]; then
|
if [ "$?" != "0" ]; then
|
||||||
@ -170,6 +183,15 @@ do
|
|||||||
echo "Unable to write to $trashdir" 1>&2
|
echo "Unable to write to $trashdir" 1>&2
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function trash_file {
|
||||||
|
filename=$1
|
||||||
|
|
||||||
|
dir=${filename%/*}
|
||||||
|
|
||||||
|
trashdir=`get_trashdir "$dir"`
|
||||||
|
init_trashdir "$trashdir"
|
||||||
|
|
||||||
trashname="${filename##*/}"
|
trashname="${filename##*/}"
|
||||||
origname="${trashname%%.*}"
|
origname="${trashname%%.*}"
|
||||||
@ -188,7 +210,7 @@ do
|
|||||||
deletedfile="$trashdir/files/$trashname"
|
deletedfile="$trashdir/files/$trashname"
|
||||||
deletedinfo="$trashdir/info/$trashname.trashinfo"
|
deletedinfo="$trashdir/info/$trashname.trashinfo"
|
||||||
|
|
||||||
canon=`url_encode $filename`
|
canon=`url_encode "$filename"`
|
||||||
|
|
||||||
cat > "$deletedinfo" <<END
|
cat > "$deletedinfo" <<END
|
||||||
[Trash Info]
|
[Trash Info]
|
||||||
@ -197,6 +219,24 @@ DeletionDate=`date +"%FT%H:%M:%S"`
|
|||||||
END
|
END
|
||||||
|
|
||||||
mv $mv_opts "$filename" "$deletedfile"
|
mv $mv_opts "$filename" "$deletedfile"
|
||||||
|
}
|
||||||
|
|
||||||
|
for f in "$@"
|
||||||
|
do
|
||||||
|
# strip quotes added by getopts
|
||||||
|
|
||||||
|
f=${f#\'}
|
||||||
|
f=${f%\'}
|
||||||
|
|
||||||
|
# get full pathname of file
|
||||||
|
|
||||||
|
filename=$(readlink -f "${f}")
|
||||||
|
|
||||||
|
yes=`can_trash "$filename"`
|
||||||
|
if [ "$yes" != "0" ]; then
|
||||||
|
trash_file "$filename"
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user