emulating rm command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting emulating rm command
# 1  
Old 07-06-2006
emulating rm command

Hi everyone!

I am trying to write a bash script to emulate the rm command, I want to save all files I delete, a bit like the recycle bin in a windows OS. My script so far is below, I still need to amend a few things, once complete I plan to alias the script to rm and use it as a safe delete method so I don't mess up and delete an important file, the thing is the part I call flow control at the bottom, its real messy and could go on forever, I want to allow for proper rm precedence when using options, can anyone give me advice on how to make a function to manage any options and arguments and reduce the amount of code I have at the bottom?

Any help or comments would be great.

Also, this is my first post so please to meet everyone!

Jack

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

# 2  
Old 07-06-2006
getopt

This may help you:
Code:
#!/bin/sh

set -- `getopt "abco:" "$@"`

a= b= c= o=
while :
do
    case "$1" in
    -a) a=1;;
    -b) b=1;;
    -c) c=1;;
    -o) shift; o="$1";;
    --) break;;
    esac
shift
done
shift # get rid of --
# rest of script...
# e.g.
ls -l $@

WARNING: spaces in filenames Smilie

It's better to use perl (more safe) and getopts() function in Getopt::Std

Even better is to change the code of original rm and make a hard link to ~/.trash/filename before unlink the file

Last edited by Hitori; 07-06-2006 at 05:21 PM..
# 3  
Old 07-06-2006
Quote:
Originally Posted by Hitori
This may help you:
Code:
#!/bin/sh

set -- `getopt "abco:" "$@"`

a= b= c= o=
while :
do
    case "$1" in
    -a) a=1;;
    -b) b=1;;
    -c) c=1;;
    -o) shift; o="$1";;
    --) break;;
    esac
shift
done
shift # get rid of --
# rest of script...
# e.g.
ls -l $@

WARNING: spaces in filenames Smilie

It's better to use perl (more safe) and getopts() function in Getopt::Std

Even better is to change the code of original rm and make a hard link to ~/.trash/filename before unlink the file

Hi,

Thanks for reply, will that give the last option precedence when there is a contradiction of options though?

I am using getops also, I have the script working a bit better but I am stock on precedence.

Thanks again

Mike
# 4  
Old 07-06-2006
got it working, like so:

Code:
#!/bin/bash
# program to emulate the "rm" command in UNIX.
# less the endless sp

#  CREATE TRASH FOLDER

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

# INITIALIZE VARIABLES
OPT=-
NO_ARGS=0
FLAG_R=""
FLAG_F=""
FLAG_I=""
FLAG_V=""
TRASH=$HOME/deleted


# FUNCTIONS

function errorInvailidOpt() {

    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 writePro () {
echo -n "rm: remove write-protected file \`$*'?"
read ANSWER
    if [ "$ANSWER" = "y" ] &&  [ "$FLAG_V" = "v" ] ; then
    mv $OPTS $@ $TRASH 2>/dev/null
    echo "removing \`$*'"
    else
    mv $OPTS $@ $TRASH 2>/dev/null

fi
}

function verbose () {
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"
}
function intVerbose () {
echo -n "rm: remove $* ?"
    read ANSWER
    if [ "$ANSWER" = "y" ] ; then
    mv  $@ $TRASH 2>/dev/null
    echo "removing \`$*'"


fi
}

function int () {
echo -n "rm: remove $* ?"
    read ANSWER
    if [ "$ANSWER" = "y" ] ; then
    mv  $@ $TRASH 2>/dev/null
fi
}

function delete() {
while :
do  case $OPTS in

      v|ivf|vf|ifv|vif) verbose $@
                      break
                      ;;
     vfi|fvi|iv|vi|fiv) intVerbose $@
                      break
                      ;;
               f|fv|if) mv -f $@ $TRASH 2>/dev/null
                      break
                      ;;
                     i) int $@
                      break
                      ;;
                     r)mv $OPTS $@ $TRASH 2>/dev/null
                      break
                      ;;
                     *)mv $@ $TRASH 2>/dev/null
                      break

esac
done

}


# GETOPTS

while getopts :rRfvi o
do    case $o in
           r|R)FLAG_R=""
             ;;
             f) FLAG_F=f
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_I=i
             ;;
             *) errorInvalidOpt

      esac
done
shift `expr $OPTIND - 1`

# FLOW CONTROL

OPTS=$FLAG_R$FLAG_F$FLAG_I$FLAG_V

if [ "$#" -eq "$NO_ARGS" ] ; then
   errorTooFew $@
elif ! [ -f  "$1" ] &&  ! [ -d "$1" ]; then
   errorNoSuch $@
elif ! [ -w  "$1" ] ; then
   writePro $@
else
   delete $@
fi

# 5  
Old 08-23-2006
Thats a really useful script Jack, saved me quite a few times now Smilie

Did you by any chance finish it so that it does all the directories stuff? ie the

Code:
rm -ir foldername

?

Thanks in advance,

Oliver
# 6  
Old 08-23-2006
Wouldn't it be just easier to simply use the mv command to move the files to whatever you designate the trash directory? Why go thorugh all of the rm simulation pain when essentially what you want to do is move a file from one directory to another?
# 7  
Old 08-23-2006
If you do not want that users remove their files accidentally, then just do not give execution rights on rm command
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using script command inside a script or emulating it

My script needs to take a folder path to the location of a C program, and run the program until the user selects exit in his C program. The script needs to create a transcript to a text file while the C program is running. Creating a transcript can be easily done with the script command but it... (2 Replies)
Discussion started by: syntax_eror
2 Replies

2. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

emulating windows like delete

Hi, Trying to create a way of emulating the windows like delete environment. I have everything working apart from deleting a single folder. Such as if I have a folder with a folder it will delete the folder inside the folder but not the pervious folder and it will delete a file win that folder... (2 Replies)
Discussion started by: limpep
2 Replies

4. UNIX for Dummies Questions & Answers

passing command output from one command to the next command in cshell

HI Guys, I hope you are well. I am trying to write a script that gets executed every time i open a shell (cshell). I have two questions about that 1) I need to enter these commands $ echo $DISPLAY $ setenv $DISPLAY output_of_echo_$display_command How can i write a... (2 Replies)
Discussion started by: kaaliakahn
2 Replies

5. UNIX for Dummies Questions & Answers

emulating cat in sed with q option

Hi, I am aware that the below are the equivalent in sed for cat command. sed ':' sed -n 'p' Is there any way to emulate the same using "q" option in sed? Thanks (8 Replies)
Discussion started by: pandeesh
8 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. Red Hat

Emulating rm command

I just need to edit the "rm" command such that it should move all the files / directories to particular folder instead of deleting. Its regarding my dessertation work , started working on the source code coreutils/src/rm , can any one guide mw to create a new source code for "rm " such that it... (0 Replies)
Discussion started by: jakris
0 Replies

8. Shell Programming and Scripting

How do I Get the Title of My Putty Window (Emulating an XTerm)

I have a Perl script that changes the terminal window title and I would like to reset it to the original value when I am done. We are using Putty which emulates xterm. We are not running X-Windows so I can't use something like xprop (can I?). I'm using XTerm control codes to change the title and I... (2 Replies)
Discussion started by: NateTut
2 Replies

9. Programming

Xlib programming - Emulating left click press/released

Hi, Lately my university asked me to develop an application to be installed on some computers for the disabled. I've developed a head tracking software (it moves the cursor on the screen following your head's movements using a webcam), and it works greatly so far except for a missing feature.... (2 Replies)
Discussion started by: BlackLight
2 Replies

10. SuSE

inconsistent ls command display at the command prompt & running as a cron job

Sir, I using the following commands in a file (part of a bigger script): #!/bin/bash cd /opt/oracle/bin ls -lt | tail -1 | awk '{print $6}' >> /tmp/ramb.out If I run this from the command prompt the result is: 2007-05-16 if I run it as a cron job then... (5 Replies)
Discussion started by: rajranibl
5 Replies
Login or Register to Ask a Question