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 |
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 |
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. |
Hi, the beginning of my code starts like this:
Code:
#!/bin/bash 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 |
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 Thanks Once again. Mike |
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). |
cheers.
how you mean? too little quote and aa/aaa/aaaa/ etc? Thanks again. Mike |
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? |
Quote:
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 |
Read carefully man bash and man getopt. There are some pitfalls.
|
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. |
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".
|
Hi, I came up with this script:
Code:
#!bin/bash 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 |
Hi,
can anyone offer any advice on the above post? cheers Mike |
All times are GMT -5. The time now is 02:47 AM. |