view shell-script @ 0:c1b3644bfc04

initial import
author Eris Caffee <discordia@eldalin.com>
date Mon, 09 May 2011 20:15:10 -0500
parents
children
line source
1 # -*- sh -*-
2 #
3 #
4 # Giving up on this version. The kind of pattern matching I really want to
5 # do against the filespces needs regular expressions, so I might was well use
6 # use perl
7 #
8 #
9 #
10 #
11 # safe-rm
12 #
13 # Put this in /root/.bashrc to help prevent accidental data loss.
14 # In any interactive shell, it aliases the rm command to a shell function
15 # that checks the arguments. Any attempt to delete something not in
16 # a subdirectory of /home will generate a warning message and prompt to
17 # continue. This should help to prevent accidentally deleting /etc or other
18 # important system files.
19 #
20 # Since the function and alias are only set for interactive shells, existing
21 # scripts will not need to be modified in any way.
22 #
23 # Also, if a user really needs to do a lot of deletion outside /home and gets
24 # annoyed by the prompt, s/he can either unalias rm or call /bin/rm directly.
25 #
26 # When installing this, be sure it comes _after_ any other "alias rm" command
27 # or else you will not be safe.
28 #
31 if [ ! -z "$PS1" ] ; then
33 function safe-rm {
35 Proceed=yes
36 Opts=""
38 while : ; do
39 case $# in
40 0)
41 break
42 ;;
43 esac
45 Option=$1
46 shift
48 case ${Option} in
49 -*)
50 # ignore option switches
51 Opts="${Opts} ${Option}"
52 ;;
53 *)
54 # Found a filespec
55 Opts="$Opts ${Option}"
56 Parent=$(dirname $(readlink -m ${Option}))
57 echo "Parent = ${Parent}"
58 if [ "${Parent%${Parent#/home}}" != "/home" ] ; then
59 Proceed=no
60 fi
61 ;;
62 esac
63 done
65 if [ "${Proceed}" = "no" ] ; then
66 echo "
68 ============ WARNING! ========== WARNING! ========== WARNING! =================
70 You are about to delete files or directories that are not in a users home
71 directory. Please review the rm command for any typos before proceeding.
73 Type \"yes\" to continue.
76 "
77 read Proceed
78 fi
80 if [ "${Proceed}" = "yes" ] ; then
81 echo "/bin/rm ${Opts}"
82 fi
83 }
85 RM_Opts=""
86 alias | grep -q "alias rm="
87 if [ $? -eq 0 ] ; then
88 RM_Opts=$(alias | awk '/alias rm=/ { sub(/^rm /, "", $2); print $2}' FS="'")
89 fi
91 unalias rm 2> /dev/null
92 alias rm="safe-rm ${RM_Opts}"
94 fi