nam3nL0s

Del - rm alias in sicurezza

May 12th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. TRASH="$HOME/.Trash"
  4.  
  5. HOP="\n\e[1;34mOPTIONS\e[m\n"
  6. HOP="$HOP""\t\e[1;34m-h\e[m\thelp\n\t\tmostra l'help ed esce\n\n"
  7. HOP="$HOP""\t\e[1;34m-v\e[m\tvisualizza\n\t\tmostra il contenuto del cestino\n"
  8. MHE="\n\t\t\e[1;37m-- DEL --\e[m\n"
  9. MHE="$MHE""\n\e[1;34mSYNOPSIS\e[m\n"
  10. MHE="$MHE""\tdel [ OPTIONS | PATHFILE1 PATHFILE2 PATHFILE 3 ... ]\n"
  11. MHE="$MHE""\n\e[1;34mDESCRIPTION\e[m\n"
  12. MHE="$MHE""\tUn cestino per terminale: 'del' non cancella i file definitivamente come 'rm',\n"
  13. MHE="$MHE""\tma li sposta nella directory Trashcan ove possono essere eventualmente recuperati.\n"
  14. MHE="$MHE""\tDirectory Trashcan (cestino) attuale: \e[1;32m$TRASH\e[m\n"
  15. MHE="$MHE""\tChiamato senza parametri 'del' svuota il cestino.\n"
  16. MHE="$MHE""$HOP"
  17.  
  18. function _err() {
  19.     if (($1)); then
  20.         me="ERR$1: "
  21.         me=$me$2
  22.         echo -e "$me" >&2
  23.         exit $1
  24.     fi
  25. }
  26.  
  27. function _help() {
  28.     echo -ne $MHE
  29. }
  30.  
  31. function _view() {
  32.     echo -e "\t-- $TRASH --"
  33.     echo "$(/bin/ls -Agh --color=always $TRASH)"
  34. }
  35.  
  36. if [ ! -d "$TRASH" ]; then
  37.     echo mkdir "$TRASH" &>/dev/null
  38.     _err $? "Non riesco a creare il cestino"
  39.     echo "Cestino creato"
  40. fi
  41. if ! (($#)); then
  42.     cd "$TRASH"
  43.     nf=$(/bin/ls -A | wc -l)
  44.     if ((nf)); then
  45.         /bin/ls -1 -A | rename 's/\.//g' &>/dev/null
  46.         _err $? "Problemi nella preparazione dello svuotamento"
  47.         rm -r * &>/dev/null
  48.         _err $? "Non riesco a svuotare il cestino"
  49.         echo "Cestino svuotato"
  50.     else
  51.         echo "Il cestino risulta vuoto"
  52.     fi
  53. else
  54.     case "$1" in
  55.         "-h" ) _help ;;
  56.         "-v" ) _view ;;
  57.         * )
  58.             mv $@ "$TRASH"
  59.             _err $? "Problemi nel cestinamento"
  60.             ;;
  61.     esac
  62. fi
  63.  
  64. exit 0
Advertisement
Add Comment
Please, Sign In to add comment