LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Emulating "rm" with a script (https://www.linuxquestions.org/questions/linux-newbie-8/emulating-rm-with-a-script-460608/)

mike9287 07-03-2006 12:29 PM

Emulating "rm" with a script
 
Hello,

I want to emulate the "rm" function in UNIX so that I can send files to a specified folder rather than delete them completely, a bit like a recycle bin. I plan to write a script then set "rm" as an alias t my script, a quesry I have is how do I find out which options have precedence over others? I need to work with options -r, -R, -f, -v, and -i is there any difference between -r and -R?

I will be using getopts for my script once I figure out how to use it.

Cheers

Mike

Tinkster 07-03-2006 01:03 PM

There's been heaps of little scripts posted here that to just that;
move stuff to somewhere else.
As for the options: -r and -R are equivalent for rm
As for the precedence: if options contradict each other the last one
on the command-line gets the win.


Cheers,
Tink

raskin 07-03-2006 01:03 PM

Well, asking about precedence is useless - try it out. Note that /bin/rm, bash rm and zsh rm can behave differently. Logically (and for /bin/rm) precedence belongs to later switch. In zsh - to -f over -i (and -v is unknown).

Read man page and experiment.

mike9287 07-03-2006 02:39 PM

Hi, the beginning of my code starts like this:

Code:

#!/bin/bash
touch /$HOME/deleted
GARBAGE=/$HOME/deleted

while getopts rRfvi option; do
    case $option in
        r) while [ $# -gt "0" ]
          do
              mv $1 $GARBAGE
              shift
              done
        ;;
        R)
            echo "testing"
        ;;
        f)
            echo "testing"
        ;;
        v)
            echo "testing"
        ;;
        i)
            echo "testing"
        ;;
        *)
            echo -n "some error code to go here"
        ;;

  esac
done


So if a user selects allias (which will be rm) and the provides option -r and then a file as an argument the script should move the file or directory to my location stored in garbage, the problem is that the 'mv' command seems to be getting the option "-r" passed to it from case and resulting in error, can anyone help me with why this might be?

TIA

mike

mike9287 07-03-2006 04:19 PM

Hi again,

I am making some progress, I have it set right now to read the argument after the the options, i still have a long way to go but I am on the right track I think, one thing I need to stop is everytime I run my script it gives an error that /$HOME/deleted is all ready created, how do I implement a stop so that it check first and then ingores if this evaluates as true?

Here is my code so far:

Code:

#!/bin/bash


function moveFiles () {

mv -f $OPTARG $GARBAGE
}


mkdir $HOME/deleted/
GARBAGE=$HOME/deleted/
NO_ARGS=0
SEPLIST="/t"


if [ $# -eq "$NO_ARGS" ]
then
  echo "rm: too few arguments"
  echo "Try \`rm --help\` for more information"
fi

while getopts :r:R:f:v:i option; do
    case $option in
        r)  SEPLIST=$OPTARG
            moveFiles
        ;;
        R)
            SEPLIST=$OPTARG
            moveFiles
        ;;
        f)
            echo "testing"
        ;;
        v)
            echo "testing"
        ;;
        i)
            echo "testing"
        ;;
        [?])
            echo  "invalid option -- $option"
            echo  "try \`rm --help\` for more information"
        ;;
  esac
done

i still need to add quite alot but can you answer the above question for now please, also notice the last part of case, I cant figure out why it will only ever spit out invalid option --"?" instead of the actually input from the user, is there awya round this?

Thanks
Once again.

Mike

raskin 07-04-2006 01:22 AM

if ![ -d "$HOME/deleted" ] ; then ... fi;
Yes, and it seems to me that you have too little quotes. Test frequently on paths like "~/a/a/a a a/a a a/a a a" in presence of "~/a/a/a" . Try once and you'll understand (or switch to zsh).

mike9287 07-04-2006 01:25 AM

cheers.

how you mean? too little quote and aa/aaa/aaaa/ etc?

Thanks again.

Mike

raskin 07-04-2006 01:53 AM

Well, try... in some cases, when $OPT is "a/a/a a a", rm $OPT is working like
rm a/a/a a a
How many arguments do you see? 3? Is it what you wanted?

mike9287 07-04-2006 02:06 AM

Quote:

Originally Posted by raskin
Well, try... in some cases, when $OPT is "a/a/a a a", rm $OPT is working like
rm a/a/a a a
How many arguments do you see? 3? Is it what you wanted?


its seems to be when I process anymore than one option then I get an error.

like if I try rm -rR file then it gives an error but if I use only one option it works fine.

thanks again for reply.

Mike

raskin 07-04-2006 02:11 AM

Read carefully man bash and man getopt. There are some pitfalls.

mike9287 07-04-2006 12:32 PM

Hello, I have managed to fix getopt but I have a problem, the file argument that comes though getopts should be assigned to $1, but when I try send it to safeDelete() as below it complains that I am missing an argument, can any one explain why?

ta

Mike

Code:

#!/bin/bash program to emulate the "rm" command in UNIX.
#

##Initialise trash folder
if ! [ -d "$HOME/deleted" ] ; then
mkdir $HOME/deleted
fi

##Initialise variables
NO_ARGS=0
FLAG_R=0
FLAG_F=0
FLAG_I=0
FLAG_S=0
TRASH=$HOME/deleted

##functions


function safeDelete() {
mv  $1  $TRASH
}



##getopts option handling
while getopts :rRfvi o
do        case $o in

          r|R) FLAG_R=1 ;;
            f) FLAG_F=1 ;;
            s) FLAG_S=1 ;;
            i) FLAG_I=1 ;;

esac
done
shift `expr $OPTIND - 1`


##flow control
if [ $# -eq "$NO_ARGS" ] ; then
echo "rm: too few arguments"
echo "try \`rm --help\` for more information"
fi

if [ $FLAG_R -eq 1 ] ; then
safeDelete
fi


raskin 07-04-2006 01:21 PM

After function call $1 is function parameter, not global. So you need to a) pass "$1" to safeDelete b) read man bash c) understand that your code will do weird things on "a a" and "a".

mike9287 07-05-2006 01:12 PM

Hi, I came up with this script:

Code:

#!bin/bash
# program to emulate the "rm" command in UNIX.


#  CREATE TRASH FOLDER

if ! [ -d "$HOME/deleted" ] ; then
    mkdir $HOME/deleted
fi

# INITIALIZE VARIABLES

NO_ARGS=0
FLAG_R=0
FLAG_F=0
FLAG_I=0
FLAG_V=0
TRASH=$HOME/deleted


# FUNCTIONS

function errorInvalidOpt() {

    echo "rm: invalid option - $o"
    echo "try \`rm -help\` for more information"
    exit 0
}


function errorTooFew() {

  echo "rm: too few arguments"
  echo "try \`rm --help\` for more information"
}

function errorNoSuch() {

  echo "rm: cannot remove $* : no such file or directory"
}

function okDelete() {

echo -n "rm: remove $* ?"
    read ANSWER
    if [ "$ANSWER" = "y" ] ; then
    mv $@ $TRASH
fi
}

function notifyDelete() {

if  [ -f  ] ; then
  echo "removing \`$*'"
  mv $@ $TRASH

fi
}

function  okNotify() {

echo -n "rm: remove $* ?"
    read ANSWER
    if [ "$ANSWER" = "y" ] ; then
    notifyDelete $@

fi
}

function delete() {
  if ! [ -f ] ; then
  errorNoSuch;
  else
  mv  $@ $TRASH
fi
}


# GETOPTS

while getopts :rRfvi o
do    case $o in

          r|R) FLAG_R=1 ;;
            f) FLAG_F=1 ;;
            v) FLAG_V=1 ;;
            i) FLAG_I=1 ;;
            *) errorInvalidOpt

      esac
done
shift `expr $OPTIND - 1`

# FLOW CONTROL

if [ "$#" -eq "$NO_ARGS" ] ; then
  errorTooFew

elif [ $FLAG_R -eq  0 ] && [ $FLAG_F -eq 0 ] && [ $FLAG_I -eq 0 ] && [ $FLAG_V -eq 0 ] ; then
  delete $@

elif [ $FLAG_R -eq  1 ] && [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
  okNotify $@

elif [ $FLAG_R -eq  1 ] && [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] ; then
  okNotify $@

elif [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
  okNotify $@

elif [ $FLAG_R -eq  1 ] && [ $FLAG_I -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
  okNotify $@

elif [ $FLAG_R -eq  1 ] && [ $FLAG_V -eq 1 ] ; then
  notifyDelete $@

elif [ $FLAG_V -eq 1 ] && [ $FLAG_I -eq 1 ] ; then
  okNotify $@

elif [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] ; then
  notifyDelete $@

elif [ $FLAG_R -eq 1 ] ; then
  delete $@

elif [ $FLAG_V -eq 1 ] ; then
  notifyDelete $@

elif [ $FLAG_I -eq 1 ] ; then
  okDelete $@

elif [ $FLAG_F -eq 1 ] ; then
  delete $@
fi

how do I get rid of all the "if then else" statements and out them in a fuction based on the options flaggged?

any help would be great, I have submitted my work and passed but I know there is an easier way that what I am doing with all them statments, I just cant figure out how.

Mike

mike9287 07-06-2006 09:50 AM

Hi,

can anyone offer any advice on the above post?

cheers

Mike


All times are GMT -5. The time now is 02:47 AM.