May 13, 2013

How to pass command line arguments to perl script


We can pass command line arguments with the labels and we can make use of Getopt::Long module for achieving this

If an user tries to pass a wrong/invalid parameter, the following will throw an error.

The following script takes the parameters like
    -str (String value)
    -how_many_times (integer)
   
The following will print the string (from -str parameter) as many times the user the entered -how_many_times parameter

pass_command_line_params.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use File::Basename;

#Extract the nae of this script. 
my $scriptname = basename($0);

my @usage =("$scriptname",
           "-str  = Enter a String",
           "-how_many_times = Enter an integer, how many times you want the String to print");

my $opt_help     = 0;
my $opt_str     = ""; 
my $opt_how_many_times  = ""; 

my $ret = &GetOptions('help',  \$opt_help,
                      'str=s',\$opt_str,
                      'how_many_times=i',\$opt_how_many_times);


# Check parameters
if (($ret != 1) || (! $opt_str) || (! $opt_how_many_times)){
    &usage();
    exit;   
}

sub usage {
    print "\n How to use this Script : ". Dumper(\@usage) . "\n";

    print "\n You didn't pass str parameter " if (!$opt_str);
    print "\n You didn't pass how_many_times parameter " if (!$opt_how_many_times);
    print "\n";

    exit;
}


for (my $i=0; $i<$opt_how_many_times; $i++) {
    print $opt_str. "\n";
}
1;  

Outout :
1) perl pass_command_line_params.pl -str=Peter -how_many_times=
    Value "dd" invalid for option how_many_times (number expected)

    How to use this Script : $VAR1 = [
              'pass_command_line_params.pl',
              '-str  = Enter a String',
              '-how_many_times = Enter an integer, how many times you want the String to print'
            ];


    You didn't pass how_many_times parameter  

2) perl pass_command_line_params.pl -str="Mother Teresa" -how_many_times=5
    Mother Teresa
    Mother Teresa
    Mother Teresa
    Mother Teresa
    Mother Teresa  



Please refer to other topics in Perl like :
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


No comments:

Post a Comment