saferm -> cestino da shell

Sab, 05/03/2005 - 15:07

saferm -> cestino da shell

Inviato da Marcoski 0 commenti

<br />#!/bin/bash<br /><br />#check if .trsh exist in home<br />if [[ ! -d ~/.trash ]]; then<br />      `mkdir ~/.trash`<br />fi<br /><br /># global variables<br />declare -a files<br />opt="0"<br />error=""<br /><br /># args handling<br />if [ -n "$1" ]; then<br />      if [[ "$1" = "-L" ]]; then<br />                opt="1"<br />   elif [[ "$1" = "-P" ]]; then<br />              opt="2"<br />   elif [[ "$1" = "-R" ]]; then<br />              opt="3"<br />           if [ -z "$2" ]; then<br />                      error="error: files missing"<br />                      opt="4"<br />           else<br />                      i=0<br />                       while [[ "$2" != "" ]]; do<br />                                dirname=`dirname $2`<br />                              if [[ "$dirname" != "." ]]; then<br />                                  files[$i]=`basename $2`<br />                           else<br />                                      files[$i]=$2<br />                              fi<br />                                let "i=$i+1"<br />                              shift<br />                     done<br />                      count=${#files[@]}<br />                fi      <br />  elif [[ "$1" = "-h" || "$1" = "--help" ]]; then<br />           opt="4"<br />   <br />  elif [[ "$1" = "-"* || "$1" = "--"* ]]; then<br />              error="error: no option found"<br />            opt="4"<br />   else<br />              i=0<br />               while [[ "$1" != "" ]]; do<br />                        dirname=`dirname $1`<br />                      if [[ "$dirname" != "." ]]; then<br />                          files[$i]=$1<br />                      else<br />                              files[$i]=`pwd`"/"$1<br />                      fi<br />                        let "i=$i+1"<br />                      shift<br />             done<br />              count=${#files[@]}<br />                opt="5"<br />        fi         <br />  <br />else<br />        error="error: no files nor opitions read"<br /> opt="4" <br />fi<br />shift<br />if [[ -n "$1" && "$1" = "-"? ]]; then<br />    error="error: impossible to combine one or more optionsi"<br /> opt="4"<br />fi<br /><br /># exec script<br />case "$opt" in<br />      "1") # show trash's files<br />         `ls ~/.trash`<br />     ;;<br />        "2") # purge trash's file<br />         `rm -rf ~/.trash/*`<br />       ;;<br />        "3") # file recovery<br />              index=0<br />           while [[ "$index" < "$count" ]]; do<br />                       if [[ -e ~/.trash/`basename ${files[$index]}` ]]; then<br />                            pathrec=`cat ~/.trash/.index | grep ${files[$index]}`<br />                             `mv ~/.trash/${files[$index]} $pathrec`<br />                           `cat ~/.trash/.index | grep -v ${files[$index]} > ~/.trash/.index`<br />                                echo "${files[$index]} recovery succesfull"<br />                       else<br />                              echo "error: file ${files[$index]} not exist in trash"<br />                    fi<br /><br />                  let "index=$index+1"<br />              done<br />      ;;<br />        "4") #help-> usage<br />                if [ -n "$error" ]; then<br />                  echo "$error"<br />             fi<br />             echo "Usage: saferm [OPTION][FILE]<br />Command line trash.<br />-L                        list all file's in the trash<br />-P                    purge all file's in the trash<br />-R                   recover a file in the trash<br />-h; --help             this menu<br />       "<br />   ;;<br />        "5") #safe rm in trash<br />    #       echo "$count"<br />             index=0<br />           while [[ "$index" < "$count" ]]; do<br />                       if [[ -e ~/.trash/`basename ${files[$index]}` ]]; then<br />                            changedate=`date -r ${files[$index]} +%s`<br />                         `mv ${files[$index]} ${files[$index]}$changedate`<br />                         files[$index]="${files[$index]}$changedate"<br />                               echo ${files[$index]}<br />                     fi<br />                        echo ${files[$index]} >> ~/.trash/.index<br />                  `mv ${files[$index]} ~/.trash/`<br />                   <br />                  let "index=$index+1"<br />              done<br /><br />esac<br /><br />


In: