Path: utzoo!attcan!uunet!husc6!ukma!psuvm.bitnet!cunyvm!nyser!cmx!jerryp
From: jerryp@cmx.npac.syr.edu (Jerry Peek)
Newsgroups: comp.unix.questions
Subject: Re: Creating a nondestructive 'rm'
Keywords: rm
Message-ID: <844@cmx.npac.syr.edu>
Date: 28 Nov 88 13:48:35 GMT
References: <4460006@hpindda.HP.COM> <67991@felix.UUCP>
Reply-To: jerryp@cmx.npac.syr.edu (Jerry Peek)
Organization: Northeast Parallel Architectures Center, Syracuse NY
Lines: 51

In article <67991@felix.UUCP> tondu@felix.UUCP (Walter Tondu) writes:
- In article <4460006@hpindda.HP.COM> burdick@hpindda.HP.COM (Matt Burdick) writes:
- Here's a solution which I use.  This comes from "Tricks of the UNIX
- Masters" by Russel G. Sage. Published by Howard W. Sams & Co.
- I have modified it a tad in order to fit my needs more exactly

- if [ "`echo \"$1\" | cut -c1`" = "-" ]
- then
-         case $1 in
-         -l)        echo "$CAN:"
-                 /bin/ls $CAN
-                 exit 0;;
-         -r)        echo "removing $CAN/*:"
-                 /bin/rm -rf $CAN/*
-                 exit 0;;
-         -f)        echo "force removal"
-                 /bin/rm -rf $@
-                 exit 0;;
-         -?)        echo "usage can [-l] [-r] file [file ...]" >&2
-                 exit 0;;
-         esac
- fi

Geez.  I don't have "Tricks of the UNIX Masters," and maybe that wasn't
the original script from the book, but it seems like that section of
the code would be a lot more efficient without the (redundant) echo/cut/test.
And it doesn't handle the -f case too well because the $@ picks up the -f.
Here's a quick hack (not tested, but should work) with a few more fixes, too:

case "$1" in
-l)        echo "$CAN:"
        /bin/ls $CAN
        ;;
-r)        echo "removing $CAN/*:"
        /bin/rm -rf $CAN/*
        ;;
-f)        echo "force removal"
        /bin/rm -r $@         # PASSES rm THE -f FROM $1
        ;;
-?)        echo "usage can [-l] [-f] [-r] file [file ...]" >&2
        exit 1
        ;;
esac
exit 0

Not to be picky here, but I think that a script that you use as much as "rm"
should be as efficient as you can make it...

--Jerry Peek, Northeast Parallel Architectures Center, Syracuse, NY
  jerryp@cmx.npac.syr.edu
  +1 315 443-1722