LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-03-2006, 12:29 PM   #1
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Rep: Reputation: 15
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
 
Old 07-03-2006, 01:03 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 07-03-2006, 01:03 PM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 07-03-2006, 02:39 PM   #4
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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

Last edited by mike9287; 07-03-2006 at 02:41 PM.
 
Old 07-03-2006, 04:19 PM   #5
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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

Last edited by mike9287; 07-03-2006 at 04:40 PM.
 
Old 07-04-2006, 01:22 AM   #6
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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).
 
Old 07-04-2006, 01:25 AM   #7
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
cheers.

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

Thanks again.

Mike

Last edited by mike9287; 07-04-2006 at 01:27 AM.
 
Old 07-04-2006, 01:53 AM   #8
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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?
 
Old 07-04-2006, 02:06 AM   #9
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 07-04-2006, 02:11 AM   #10
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Read carefully man bash and man getopt. There are some pitfalls.
 
Old 07-04-2006, 12:32 PM   #11
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 07-04-2006, 01:21 PM   #12
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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".
 
Old 07-05-2006, 01:12 PM   #13
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 07-06-2006, 09:50 AM   #14
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi,

can anyone offer any advice on the above post?

cheers

Mike
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 09:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration