[comp.lang.perl] simple script

kpc@pluto.arc.nasa.gov (kpc) (05/15/91)

I'm curious about perl and wonder what an equivalent to the following
small sh safe-rm script would look like.  How cleanly can it be
written, I wonder, and how efficiently?  Would you use perl?

On a different subject, I wonder whether anybody has considered using
something like perl as a shell.

Please reply by email if you can.

:
#
#copy
#
#non destructive cp and mv and ln.
#
#many improvements are possible, such as allowing arguments, figuring
#out when and how to copy directories, cleaning it up, handling
#special kinds of files and maybe file names, and making it
#more efficient.
#

#getopts
#CMD=/bin/`basename $0`

trashdir=${TRASHDIR:?`no trashdir`}

if [ "$1" = '-R' ]      ##rm
then
  /bin/mv $* $trashdir
  exit 0
elif [ "$1" = '-m' ]    ##mv
then
  CMD=/bin/mv
  shift
elif [ "$1" = '-l' ]    ##ln
then
  CMD=/bin/ln
  shift
else                    ##cp
  CMD=/bin/cp
fi

if [ $# -lt 2 ]
then
  echo usage: cp file1 file2   or   cp file+ dir 1>&2
  echo usage: -m moves; -l hard-links.  \(too few args\) 1>&2
  exit 1
fi

#${`eval $#`}
for i
do
  lastarg=$i
done

if [ ! -d $lastarg ]
then

  if [ $# -ne 2 ]
  then
    echo usage: cp file1 file2   or   cp file+ dir 1>&2
    echo usage: -m moves; -l hard-links.  \(too many args\) 1>&2
    exit 1
  fi
  
  if [ -f $2 -a -f $1 ]
  then
#copy preserves permissions of target
#this can be more efficient
    echo kcp: copying $2 to trash first. 1>&2
    /bin/cp $2 $trashdir
  fi
  $CMD $1 $2
else
  for eachfile
  do
    if [ $eachfile != $lastarg ]
    then
      file=$lastarg/`basename $eachfile`
      if [ -f $file -a -f $eachfile ]
      then
#mv and chmod?
        echo kcp: copying $file to trash first. 1>&2
        /bin/cp $file $trashdir
      fi
      $CMD $eachfile $file
    fi
  done
fi