Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: jpeters on November 20, 2009, 01:42:52 PM

Title: trash
Post by: jpeters on November 20, 2009, 01:42:52 PM
Moves file/folder to trash directory with $1 (eg, "trash myfile").  Without $1 (eg, "trash"), prompts
for deleting.  Edit PATH (script will create/remove directory).  

For use with DFM, create icon. Set shellcommand to "aterm -e !0!".  Drag files to icon (move), or click for viewing/deleting.

Code: [Select]
#!/bin/ash


#### trash.sh script, jeters
###  deletes $1 files/folders; without $1, prompts to view/delete

## Edit PATH:
TRASH="/mnt/hda3/tmp/trash"

. /etc/init.d/tc-functions

[ -d /mnt/hda3/tmp/trash ] || sudo mkdir -p ${TRASH}

if [ $1 ]; then
  sudo mv "$@" ${TRASH}
else
  if [ "$(ls ${TRASH})" ]; then
     clear
     ls ${TRASH}
     echo "_____________"
     echo ${GREEN}"delete? (y)"${NORMAL}
     read RESPONSE
       case ${RESPONSE} in
         "y" )  sudo rm -R ${TRASH} ; echo ${RED}"deleted!" ${NORMAL}; sleep 1 ;;
         "*" )  break ;;
       esac
   else
     echo ${GREEN}"Empty" ${NORMAL}; sleep 1
   fi

fi

exit 0
      
     
      
Title: Re: trash
Post by: jpeters on November 20, 2009, 01:55:57 PM
screenshot with DFM

[removed due to policy]apps/trash.png
Title: Re: trash
Post by: jpeters on November 22, 2009, 04:39:36 AM
Updated to more user friendly version.
Title: Re: trash
Post by: jpeters on November 22, 2009, 04:37:35 PM
updated to include multiple arguments: eg, "trash file1 file2 mydir";  also "trash myfiles*"

Title: Re: trash
Post by: mikshaw on November 23, 2009, 09:01:48 AM
Code: [Select]
  while [ $# -ge 1 ];do
     sudo mv ${1} ${TRASH}
     shift
   done
Could this be done in just one command rather than a loop?  The mv command supports mulitple files.
Code: [Select]
sudo mv "$@" ${TRASH}
The directory test is using a specific path rather than $TRASH

The delete confirmation could be improved.  If you just press enter when the "y" is visible, you get an error.  It is common to have a default selection displayed in uppercase -- Delete? (y|N) -- which allows the user to just press enter to accept the default.  The error can be avoided either by first testing for the existence of $RESPONSE before comparing it to "y", or by using case instead of if.

Personally I wouldn't delete the trash directory, only its contents...but I can see reasons for doing that.
Title: Re: trash
Post by: jpeters on November 23, 2009, 12:51:13 PM
Code: [Select]
 while [ $# -ge 1 ];do
     sudo mv ${1} ${TRASH}
     shift
   done
Could this be done in just one command rather than a loop?  The mv command supports mulitple files.
Code: [Select]
sudo mv "$@" ${TRASH}
The directory test is using a specific path rather than $TRASH

The delete confirmation could be improved.  If you just press enter when the "y" is visible, you get an error.  It is common to have a default selection displayed in uppercase -- Delete? (y|N) -- which allows the user to just press enter to accept the default.  The error can be avoided either by first testing for the existence of $RESPONSE before comparing it to "y", or by using case instead of if.

Personally I wouldn't delete the trash directory, only its contents...but I can see reasons for doing that.

Thanks mikshaw. Always great to have your expertise.  I removed the loop and fixed the bug, as you suggested. The (y|N) looks like (yIN) on my computer, so others can add that if they want..although it's not needed.  I  kindof like deleting the whole directory, since it's easier to move around, etc., but that could be edited:  "y" )  sudo rm -R ${TRASH}/*  ....