run a perl with option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting run a perl with option
# 1  
Old 03-31-2008
run a perl with option

I have a perl script which runs and send out e-mail if duplicates are found.

I would like to run the with option (like -e) so that it will produce the out put only and will not send out e-mail.

How can I achieve it. I would appreciate the help.
Thanks.

Example: ./file1 (sends out e-mail)

./file1 -e (will not send out e-mail just will show the output in the screen).


Here is the script:
Code:
use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicate_ip, $duplicate_host);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
   if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
       s/\s+\b/\t/g; 
      push @{$ip{$ip}}, $_;
      push @{$host{$host}}, $_;
   }
}
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.
\n
************Duplicate IP addresses**************:
\n
$duplicate_ip
************Duplicate Hostnames*****************:
\n
$duplicate_host
EMAIL_MSG
print $email_msg;
my $email = MIME::Lite->new(
                          From => 'xxx@xxx.com',
                          To => 'xxx@xx.com',
                          Cc => 'xxy@xx.com,xxz@xx.com',
                          Subject => 'Host file duplicates',
                            Data => $email_msg
                            );
if ($duplicate_ip || $duplicate_host) { 
$email->send } #send email if duplicate found


Last edited by Yogesh Sawant; 03-31-2008 at 12:05 PM.. Reason: added code tags
# 2  
Old 03-31-2008
make use of the module Getopt::Long to accept command line options in your script

then, based on whether the required option is set or not, decide whether to send e-mail or not
# 3  
Old 03-31-2008
Can it be without installing any module? Thanks
# 4  
Old 03-31-2008
Quote:
Originally Posted by amir07
Can it be without installing any module? Thanks
Yes, just drop the - and use:

Code:
perl scriptname e

then in your perl program get the argument from the @ARGV array:

Code:
my $option = $ARGV[0];

then check the value of $option and take whatever action is required.
# 5  
Old 03-31-2008
Quote:
Originally Posted by amir07
Can it be without installing any module? Thanks
Getopt::Long is part of the standard Perl distribution. It should normally be available on any machine with Perl installed. You don't need to install anything extra.

It's better not to reinvent the wheel by manually parsing @ARGV if a core module will do what you want.
# 6  
Old 04-01-2008
Well, you still have to parse/check the input regardless of if you directly parse @ARGV or use the module. There is no reinvention there. That module is to extend the command line options so you can use POSIX syntax with GNU extensions. That being said, I would urge the OP to use the module just to learn the interface for future programs that can really take advantage of it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

3. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

4. Shell Programming and Scripting

Perl debuggin option

I am getting out of memory issue in perl. I need to debug which function taking more memory constraints. what the commands to find out the memory consuming in the perl program. (1 Reply)
Discussion started by: ramkumar15
1 Replies

5. Shell Programming and Scripting

[Perl] Command option with optional value.

Hi, I would like to parse command line arguments. The problem I am facing is that I cannot get it right when an option has a optional value. Example: # ./ej.pl --remove # ./ej.pl --remove all And the script may allow only one option. So this is wrong: # ./ej.pl --dummy --remove I... (3 Replies)
Discussion started by: ejdv
3 Replies

6. Shell Programming and Scripting

sh file: READ (menu) but now run with option

I have a script which uses READ to detect choice of menu option...now I want to change the script without doing whole rewrite such that when user runs ./script.sh 5 it would execute menu option 5 rather than user running ./script.sh waiting for it to load and then pressing "5 enter" Is it... (1 Reply)
Discussion started by: holyearth
1 Replies

7. Shell Programming and Scripting

Better way to run this perl command

i'm working with files that are huge in size. over 3GB. and i need to do a lot of pattern matching. I need a way to grep for what i want, using a tool that is available across most unix systems. i initially was gungho about grep, but not all capablities of grep are available on all OSes. so... (10 Replies)
Discussion started by: SkySmart
10 Replies

8. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

9. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question