emulating rm command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting emulating rm command
# 8  
Old 08-29-2006
Quote:
Originally Posted by olimiles
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
Hi Oliver,
I did finish it.

Here is the final copy.

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

# INITIALIZE VARIABLES
NO_ARGS=0
FLAG_R=""
FLAG_F_I=""
FLAG_V=""
TRASH=$HOME/deleted

# FUNCTIONS

function errors() {
if [ "$#" -eq  "$NO_ARGS" ] ; then
      echo "rm: too few arguments"
      echo "Try \`rm --help' for more information."
      exit 0
elif [[  ! -f "$1"  && ! -d "$1"   ]] ; then
      echo "rm: cannot remove $ARG : no such file or directory"
      exit 0
elif [[ -d $ARG  &&  "$FLAG_R" = ""  ]] ; then
      echo "rm: \`$ARG' is a directory"
      exit 0
else
     checkExisting $1
fi
}


function checkExisting (){
if [ -d $TRASH/$1 ]; then

     find "$TRASH/$1" -type f -exec shred -fu {} \; 2>/dev/null
     find "$TRASH/$1" -type d -exec rmdir -p  {} \; 2>/dev/null
     directoryDelete $1
else
     directoryDelete $1

fi

}

function directoryDelete () {
if [[ -d "$1"  &&  "$FLAG_R" == "R" && "$FLAG_F_I" == "i" ]]; then
echo -n "rm: descend into directory \`$1'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
echo  "removing all entries of directory \`$1'?"

for FILE in $1/*
do
       if [ -d $FILE ] ; then
       directoryDelete $FILE
   else
       writePro $FILE
fi
done
     echo -n "rm: remove directory \`$1'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     mv  $1 $TRASH 2>/dev/null
echo "rm: removing directory itself: \`$1'"
fi
fi
else
    writePro $1
fi
}

function writePro () {
if ! [ -w  "$1" ] ; then
echo -n "rm: remove write-protected file \`$*'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     delete $1
     fi
else
     delete $1
fi

}

function delete() {
if [ "$FLAG_F_I" = "i" ] &&  [ -w  "$1" ] ; then
   interactive $1
elif [ "$FLAG_F_I" = "f"  ] ; then
   force $1
elif [ "$FLAG_R" = "R"  ] ; then
   remove $1
else
   remove $1
fi

}

function force () {

mv -f $1 $TRASH 2>/dev/null
verbose $1
}


function remove () {
mv  $1 $TRASH 2>/dev/null
verbose $1
}

function interactive () {
echo -n "rm: remove $1 ?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     remove $1
     else
     exit 0
fi
}
function verbose () {
if [ "$FLAG_V" = "v" ] ; then
if [ "$FLAG_V" = "v" ] ; then
echo "removing \`$1'"
fi
}


# PARSE OPTIONS WITH GETOPTS

while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=R
             ;;
             f) FLAG_F_I=f
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_F_I=i
             ;;
             *) echo "rm: invalid option -$1"
                echo "try \`rm --help' for more information"
                exit 0
      esac
done
shift `expr $OPTIND - 1`

# START OF FLOW

if ! [ -d "$HOME/deleted" ] ; then
     mkdir $HOME/delete
elif  [ $# -eq $NO_ARGS ] ; then
errors
else
   for ARG in $@
   do
   errors $ARG
   done
fi

Regards

Jack
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