Web Hosting Talk







View Full Version : Help with a bash script


jayjay
08-02-2002, 02:27 AM
I've been learning some bash scripting in the past few weeks, but I'm stumped here.

I want to create a script to execute multiple "locate" commands.
Such as:

locate blah.cgi

locate mypornsafe.exe

etc. Then email me the results. I would have to cronjob it, and I think it would be set.

I'm basically stuck on, is it possible to email me the results? Or would it be better to dump it to a file?

Thanks on any input.

prime
08-02-2002, 02:42 AM
locate blah.cgi | mail root@domain.com

That should do the trick. It's one way of doing it. You can send the output to a file instead (locate blah.cgi > results.txt) and follow it by a 2nd command (mail root@domain.com < results.txt I *think* - see 'man mail').

I know mail has a few command line options too. To add a subject for example.

jayjay
08-02-2002, 03:03 AM
Right now.. I have it setup like this (just a few for example):

jayjay
08-02-2002, 03:06 AM
Right now.. I have it setup like this (just a few for example):

#!/bin/sh
# locate and email

locate xxx.cgi | mail -s subject user @ 404labs. com
locate bbb.cgi | mail -s subject user @ 404labs. com
locate eee.pl | mail -s subject user @ 404labs. com

(I put spaces in the email for "protection of spam bots")

The only problem with this is it has to send a email for each script it searches for with the results. Otherwise I wouldn't know what the search was for, but I suppose the filename would tell me.

I think that's the most efficent way to have it, but sometimes I'm silly.

The Prohacker
08-02-2002, 03:11 AM
I know how todo this in PHP, but I'm stumped on shell..

I know there is a way though :D

GH_Dave
08-02-2002, 03:37 AM
locate xxx.cgi;locate bbb.cgi;locate eee.pl | mail -s subject user @ 404labs. com

This should do the trick for you, that's a semi-colon in between each.

Dave

jayjay
08-02-2002, 03:39 AM
Duhh.. I need to put on some coffee.

Thanks alot Dave : )

GH_Dave
08-02-2002, 03:41 AM
No prob - I'm just happy that I learned something new today, and the day is only 40 minutes old! I guess I can relax now :D

Dave

jayjay
08-02-2002, 03:46 AM
Now the only problem when I convert it too:

locate blah.cgi;locate blah2.cgi;locate blah3.cgi;locate lstmrge.cgi | mail -s subject jay@404labs.com

It will find something, but send a blank email. Now I'm really stumped. heh.

GH_Dave
08-02-2002, 03:59 AM
Whoops, I spoke too soon eh? I suppose you could redirect that to a file; compare the size of it; email if greater than zero? But that starts to get messy!

jayjay
08-02-2002, 04:02 AM
I'm not even outputting to a text file.

root@b0x0r [~]# ./locate.sh
/home/user/public_html/forums/ultimatebb.cgi
Null message body; hope that's ok
Null message body; hope that's ok
Null message body; hope that's ok
root@b0x0r [~]#

Now I moved it from having sending one email to one email for each type of script.

(3 Currently)

But they're all blank. But one result came back as you can see.

priyadi
08-02-2002, 04:48 AM
Originally posted by jayjay
Now the only problem when I convert it too:

locate blah.cgi;locate blah2.cgi;locate blah3.cgi;locate lstmrge.cgi | mail -s subject jay@404labs.com

It will find something, but send a blank email. Now I'm really stumped. heh.

The problem is only the last command (locate lstmrge.cgi) will be sent by email. You need to group those locate commands by using colons:


(locate blah.cgi; locate blah2.cgi locate blah3.cgi ) | mail -s subject jay@404labs.com

BMurtagh
08-02-2002, 01:24 PM
i don't know if you've fixed it yet, but in your above post you had:

locate blah.cgi;locate blah2.cgi;locate blah3.cgi;locate lstmrge.cgi | mail -s subject jay@404labs.com

now i'm just starting to learn scripting, but i think if you had the results sent to a file & mailed the file, it'd work better for you sending only 1 mail, and it'd be less reading since they'd all be listed in 1 mail.

it also looks like you're sending it an empty mail, that is why i agreed with the person who said to use results.txt. hope it helps

Ahmad
08-02-2002, 01:34 PM
As a side note: you might want to do an 'updatedb' before the locate's line.