Perl or Shell Script to read files

Hi,
 
Could anyone please help me with the code snippet for the below logic.

LOGIC:
 Need to grep a file from the path "/usr/qa/qaout.log' and open  and read  the file to grep for words like Error, Permission Denied, Could not send, No such file or directory.

If it encounters any one of these word it needs to send and  email with this log file as an attachment.

Thanks,
new_perl_userAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryan ButlerCommented:
How about:
#!/usr/bin/perl -w
use strict;

sub send_me_email {
  use MIME::Lite;

  my $msg = MIME::Lite->new(
    From    => 'from@myhost.com',
    To      => 'to@yourhost.com',
    Cc      => 'others@other.com, other2@more.com',
    Subject => 'Log file attached...',
    Type    => 'multipart/mixed',
  );

  $msg->attach(
      Type     => 'TEXT',
      Data     => "Here's the Log file",
  );

  $msg->attach(
    Type     => 'image/gif',
    Path     => 'aaa000123.gif',
    Filename => 'logo.gif',
  );

  $msg->send;
}

open (TEXTFILE, '/usr/qa/qaout.log');
$_ = join('',<TEXTFILE>);
close (TEXTFILE)

while (<>) {
  send_me_email if /Error|Permission Denied|Could not send/;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bryan ButlerCommented:
 $msg->attach(
    Type     => 'image/gif',
    Path     => 'aaa000123.gif',
    Filename => 'logo.gif',
  );

 should be:

$msg->attach(
    Type     => 'text',
    Path     => '/usr/qa',
    Filename => 'qaout.log',
);

0
TintinCommented:
Assuming you have mutt (there are plenty of alternatives)

#!/bin/bash
LOG=/usr/qa/qaout.log
egrep -qi "error|permission denied|could not send|no such file or directory" $LOG && mutt -a $LOG -s "Subject line" add@example.com
~

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.