Art Alexion on 1 Feb 2006 19:45:33 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Interactive rm


bergman@merctech.com wrote:

>the original posted requested a slightly 
>different behavior, as in:
>	if there are multiple arguments to rm, then run rm interactively
>	if there is a single argument to rm, then run rm non-interactively
>
>This is not as trivial as it appears. While it's easy to count the number of 
>arguments and call "rm -i" if there's more than one argument, this doesn't deal 
>correctly with arguments that are options, not the targets of the rm command. 
>In addition, rm is sensitive to the order of arguments, and it's easy to get 
>unexpected results when combining "-f" and "-i". I'm also skipping over the 
>issues with targets that have embedded whitespace in their names...
>
>=> > How can I restore rm's default behavior so that "rm *" requires
>=> > confirmation, but "rm specified_file" does not?
>
>Here's a bash shell function that will do what you want. This fragment of code 
>can be put in your ~/.bashrc file. Note that the function, as named, will work 
>when called as "myrm". You can change this to "rm" and therefore hide how "rm" 
>really behaves.
>  
>
Thank you Mark.  That does the trick.  A couple of test yield the
following, though:

    * it seems, as written, it is called with 'saferm', not 'myrm', right?
    * though it doesn't treat options as file arguments, it does not
      pass the options to rm, so 'saferm -f *' still runs rm as 'rm -i *'
    * I wanted to rename it just 'rm', but then presumed that the 'rm
      wildcard' stuff in some of my scripts  would start to require
      confirmation which I could not force with 'rm -f wildcard'
    * I knew it would be tough to change my habit of just typing rm to
      typing saferm (and the evil I am trying to avoid is habit driven),
      so I renamed it 'del' as in DOS del (I often type rm in a win
      command shell as it is) which is just regaining an old habit.
    * a weird observation.  I have three shells open.  I did my
      experiments with one of the shells only, i.e. I only ran the
      command 'source ~/.bashrc' in one of the shells.  In that shell, I
      ran the function after renaming it 'rm'.  After I found out that
      'rm -f *.flub' no longer overrode the -i option, I changed the
      name to del and ran 'source ~/.bashrc' again.  But in that shell,
      rm still asks for confirmation, even though del works, too.  In
      the other shells, I only ran the source command only after I was
      done editing, and rm works as it does by default.
    * You are right that it doesn't work with files with embedded spaces.

Thanks, again.  I really appreciate the help.
 

>------------------------------------------------------------------------------
>function saferm
>{
>        # Shell function to call rm interactively if there are multiple
>        # non-option arguments to rm.  This is somewhat complicated due
>        # to the need to separate option arguments from targets,
>        # and for the need to deal with the special case of the "--"
>        # argument.
>        #
>        # $Header: /home/bergman/Bin/RCS/saferm,v 1.2 2006/02/01 18:11:57 bergman Exp bergman $
>        #
>        # Copyright 2006, Mark Bergman
>        # bergman@merctech.com
>        # Released under the GNU GENERAL PUBLIC LICENSE, Version 2
>        targetcount=0
>        targets=""
>        options=""
>
>        for arg in $*
>        do
>                case $arg in
>                        --)
>                                # signifier of last option argument,
>                                # successive filename arguments may begin with
>                                # a "-", so copy all remaining arguments into the
>                                # list of targets
>                                temp=`echo $* | sed -e "s/.* -- //"`
>                                targets="$targets $temp"
>                                targetcount=$((targetcount + 1))
>                                last
>                                ;;
>
>                        -*)
>                                # We have an option argument...
>                                options="$options $arg"
>                                ;;
>
>                        *)
>                                # non-option argument...copy it into the
>                                # list of file/directory names
>                                #
>                                # Note: this will probably break (badly)
>                                # if used with filenames with embedded
>                                # whitespace. It's possible to munge IFS
>                                # to get around some of that...
>                                targets="$targets $arg"
>                                targetcount=$((targetcount + 1))
>                                ;;
>                esac
>        done
>
>        if [ $targetcount -gt 1 ]
>    then
>                # Note: this weirdness is due to the fact that rm applies
>                # option arguments in the order that they are found, with
>                # successive arguments overriding previous ones. Thus, if
>                # one of the arguments is "-f" (force non- interactive),
>                # then the "-i" must be the last argument
>
>                /bin/rm $options -i $targets
>        else
>                /bin/rm $*
>        fi
>}
>
>------------------------------------------------------------------------------
>Mark
>  
>


-- 

_______________________________________
Art Alexion
Arthur S. Alexion LLC

PGP fingerprint: 52A4 B10C AA73 096F A661  92D2 3B65 8EAC ACC5 BA7A
The attachment -- signature.asc -- is my electronic signature; no need for alarm.
Info @ http://mysite.verizon.net/art.alexion/encryption/signature.asc.what.html

Key for signed PDFs available at
http://mysite.verizon.net/art.alexion/encryption/ArthurSAlexion.p7c
The validation string is TTJY-ZILJ-BJJG.
________________________________________

Attachment: signature.asc
Description: OpenPGP digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug