Register
It is currently Sun Sep 14, 2014 10:03 pm

rmstar


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Fri Aug 26, 2011 9:25 am   

Joined: Fri Aug 26, 2011 9:06 am
Posts: 1
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 $@"   #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 $@  #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
}                             


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP