The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conditional FTP Dastard Shell Programming and Scripting 2 06-21-2008 07:38 AM
AWK - conditional cause Rafael.Buria Shell Programming and Scripting 2 01-28-2008 01:24 PM
conditional statement lalelle Shell Programming and Scripting 8 08-21-2007 08:57 AM
conditional split braindrain Shell Programming and Scripting 5 03-11-2006 03:54 AM
Conditional Statements cstovall Shell Programming and Scripting 1 05-15-2005 05:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-05-2008
ricky007 ricky007 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 9
Smile conditional email

Some one please help me: I need to perl code for a conditional block:

I believe I need to put the $email->send; command in a conditional block that checks if $duplicate_ip or $duplicate_host is defined, and then send the email. How can I do that:

use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicate_ip, $duplicate_host, @linevals, $outline);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
chop;
if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
@linevals = split;
$outline = $linevals[0] . "\t" . $linevals[1] . "\n";
push @{$ip{$ip}}, $outline;
push @{$host{$host}}, $outline;
}
}
close $file;
#print "Duplicate IP's with hostnames\n";
foreach my $ip ( keys %ip ) {
if ( @{$ip{$ip}} > 1 ) {
$duplicate_ip .= join ('', @{$ip{$ip}}) . "\n\n";

}
}
#print "\nDuplicate hostnames with IP's\n";
foreach my $host ( sort keys %host ) {
if ( @{$host{$host}} > 1 ) {
my ($ip) = $host{$host}[0] =~ /^#?([\d.]+)/;
unless ( @{$ip{$ip}} > 1 ) {
$duplicate_host .= join('', @{$host{$host}}) . "\n\n";
}
}
}

my $email_msg = <<EMAIL_MSG;
The following entries in the host file are duplicates
either by IP address or by hostname.
Duplicate IP addresses:
$duplicate_ip
Duplicate Hostnames:
$duplicate_host
EMAIL_MSG
print $email_msg;
my $email = MIME::Lite->new(
From => 'xxx@xxx.com',
To => 'xxxx@xxx.com',
#Cc => 'xxx@xx.com,xxxx@att.com',
Subject => 'Host file duplicates',
Data => $email_msg
);
$email->send
  #2 (permalink)  
Old 03-05-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
The code's too hard to make out with the CODE tages around it but it sounds like you need this:
Code:
if ($duplicate_ip || $duplicate_host) {
  stuff
}
  #3 (permalink)  
Old 03-05-2008
ricky007 ricky007 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 9
Red face

Is this code goes after $email->send:

if ($duplicate_ip || $duplicate_host) then
$email->send

bottom line is e-mail will be send if duplicate is found otherwise don't send an email.

thanks

Last edited by ricky007; 03-06-2008 at 12:18 AM..
  #4 (permalink)  
Old 03-06-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Quote:
Originally Posted by ricky007 View Post
Is this code goes after $email->send:

if ($duplicate_ip || $duplicate_host) then
$email->send

bottom line is e-mail will be send if duplicate is found otherwise don't send an email.
Looks about right, although perl uses { and } rather than 'then'
And yep, if either of the two duplicate_xxx variables have something set in them it'll send the email, if both of them are empty it won't do anything.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0