![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help Required: Command to find IP address and command executed of a user | loggedout | Security | 2 | 08-06-2008 05:12 PM |
| how to? launch command with string of command line options | TinCanFury | Shell Programming and Scripting | 5 | 04-28-2008 03:06 PM |
| inconsistent ls command display at the command prompt & running as a cron job | rajranibl | Linux | 5 | 07-30-2007 05:26 AM |
| How to use more than one MPE command STREAM with Unix command in a single shell? | bosskr | HP-UX | 1 | 10-16-2006 01:16 PM |
| How to use more than one MPE command STREAM with Unix command in a single shell? | bosskr | Shell Programming and Scripting | 0 | 09-19-2006 06:44 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 $@
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 01:21 PM. |
|
#3
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Thats a really useful script Jack, saved me quite a few times now
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
|
||||
|
||||
|
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
|
||||
|
||||
|
If you do not want that users remove their files accidentally, then just do not give execution rights on rm command
|
||||
| Google The UNIX and Linux Forums |