Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rm alias

Status
Not open for further replies.

afifss

MIS
Joined
Jan 4, 2011
Messages
8
Location
EG
hi ,

i want to make alias for rm to be like that :

alias rm 'rm -iv $A | mailx -s "file $A deleted" sherif@hotmail.com'

where A is the input file i will run the rm command on it as example

rm sherif so when that go to the alias it should run like that :

rm -iv $A (where A will bethe input file sherif) | mailx -s "file $A (where A will bethe input file sherif) deleted" sherif@hotmail.com


in another word i want to make that input file name sherif to be variable for the alias of the rm command .


hope you can get what i mean , please feed me back how i can make it i need it for emergency thing thanks
 
Why are you piping the output of the rm command into the email? Normally rm doesn't display anything. No harm in doing it though I suppose.

Aliases are not ideal when you need to use parameters in the middle of the command; instead you should try using a function.

Remember also that rm isn't always just used to remove one file, and sometimes people will use options like -rf, so you should use "$@" to include all supplied parameters.

Code:
function rm
{
    rm -iv "$@"
    mailx -s "files $@ deleted" sherif@hotmail.com </dev/null
}

This also assumes that the rm commanded succeeded, which is not necessarily true.

Annihilannic.
 
Annihilannic said:
Normally rm doesn't display anything.

I guess the -v option probably makes it display something...

Annihilannic.
 
thanks for your help , i think making alias will not work good ,

just the problem i wanna make alias to send email when ever any one use rm command in my network , wanna monitor the rm command so do you have any way to do that ?
 
A function, like I suggested... did you try it?

Annihilannic.
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
sorry i just see your replay , can feed me back how to use the functions ?
 
What shell are you using? (i.e. what is the output of ps -fp $$?

Try pasting the example I gave above into your ~/.profile, then log out and log back in again.

Type typeset -f to see what functions you have loaded to make sure it has loaded properly.

Alternatively you can put it in a separate file, for example myfuncs.sh, and then load it like this:

Code:
. ./myfuncs.sh

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top