BashScripts.org
http://bashscripts.org/forum/

rmstar
http://bashscripts.org/forum/viewtopic.php?f=16&t=1446
Page 1 of 1

Author:  videokunst [ Fri Aug 26, 2011 9:25 am ]
Post subject:  rmstar

hi freaks!

for the tcsh shell there exist a switch called 'rmstar' which prevents accidentally removing too much.

if you're using bash and you also wish to receive a warning when a star * is included in your deletion command, add the following to your .bashrc:

have fun with it.

Code:
alias rm="set -f; rmstar [email protected]"   #set -f to stop * from  being expanded                                       

rmstar () {
        checkstar ${@}  #check whether * is in argument
        if [ ${PROCEED} != 'y' ]
            then
            inquire ${@}  #if * found the user is asked whether he/she wants to continue
        fi
        if  [ ${PROCEED} == 'y' ]
            then set +f  #enable expanding * again
                \rm [email protected]  #backslash by-passes alias
        fi
}

inquire () {
        echo -e "removing \e[1;32m ${RECUR} \e[0;31m ${@} \e[0m"
        echo -n 'Are you sure? [y/N] '
        read INQ
        if [ ${#INQ} -ne 0 ] 
            then PROCEED=${INQ}
        else  #if nothing entered by user length of variable is zero
            PROCEED='N'
        fi
}
checkstar () {
        PROCEED='y'  #inquire whether to continue removing
        RECUR=''       #inquire whether -r is set as an option
       
        if [[ $1 == -* ]]
            then
            if [ `expr index $1 'r'` -ne 0 ]
                then RECUR='recursively'
            fi
        fi

        for arg in ${@}
        do
                if [ `expr index ${arg} '*'` -ne 0 ]
                        then PROCEED='N'
                fi
        done
}                             

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com