-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm_secure.sh
executable file
·107 lines (98 loc) · 2.61 KB
/
rm_secure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
sauvegarde_rm=~/.rm_saved/
function rm
{
local opt_force=0
local opt_interactive=0
local opt_recursive=0
local opt_verbose=0
local opt_empty=0
local opt_list=0
local opt_restore=0
local opt
OPTERR=0
#Parsing Command Line Arguments
while getopts ":dfirRvels-:" opt ; do
case $opt in
d) ;; #Ignored
f) opt_force=1 ;;
i) opt_interactive=1 ;;
r | R) opt_recursive=1 ;;
e) opt_empty=1 ;;
l) opt_list=1 ;;
s) opt_restore=1 ;;
v) opt_verbose=1 ;;
-) case $OPTARG in
directory) ;;
force) opt_force=1 ;;
interactive) opt_interactive=1 ;;
recursive) opt_recursive=1 ;;
verbose) opt_verbose=1 ;;
help) /bin/rm --help
echo "rm_secure:"
echo " -e --empty Empty the trash"
echo " -l --list View saved files"
echo " -s --restore Recover files"
return 0 ;;
version) /bin/rm --version
echo "(rm_secure 1.2)"
return 0 ;;
empty) opt_empty=1 ;;
list) opt_list=1 ;;
restore) opt_restore=1 ;;
*) echo "illegal option -$OPTARG"
return 1 ;;
esac ;;
?) echo "illegal option -$OPTARG"
return 1 ;;
esac
done
shift $(($OPTIND - 1))
#Possibly create the directory
if[ ! -d "$sauvegarde_rm" ] ; then
mkdir "$sauvegarde_rm"
fi
#Empty the trash
if [ $opt_empty -ne 0 ] ; then
/bin/rm -rf "$sauvegarde_rm"
return 0
fi
#List of saved files
if [ $opt_list -ne 0 ] ; then
( cd "$sauvegarde_rm"
ls -lRa * )
fi
#File recovery
if [ $opt_restore -ne 0 ] ; then
while [ -n "$1" ] ; do
mv "${sauvegarde_rm}/$1"
shift
done
return
fi
#Deleting files
while [ -n "$1" ] ; do
#Interactive deletion: ask the user
if [ $opt_force -ne 1 ] && [ $opt_interactive -ne 0 ]
then
local response
echo -n "Destroy $1 ? "
read response
if [ "$response" != "y" ] && [ "$response" != "Y" ]
[ "$response" != "o" ] && [ "$response" != "O" ]
shift
continue
fi
fi
if [ -d "$1" ] && [ $opt_recursive -eq 0 ] ; then
#Directories requires recursive option
shift
continue
fi
if [ $opt_verbose -ne 0 ] ; then
echo "Deleting $1"
fi
mv -f "$1" "${sauvegarde_rm}/"
shift
done
}
trap "/bin/rm -rf $sauvegarde_rm" EXIT