?
Solved

Multiple Parameters

Posted on 2007-07-24
8
Medium Priority
?
179 Views
Last Modified: 2010-04-16
Hi guys hope you can help.
Id like to be able to do build the following script that does the following..

checkfile.pl -b <business> -s <scope>  -n <remote file> -f <filename>
where:
<business>, <scope>, <remote file> and <filename> are parameters that are entered in by the user.

Options available would be:

<business>:      -b
"L" --- (for liquor),   # any systems in <filename> that start with the letter "l"
"S" --- (for store),   # any systems in <filename> that start with the letter "s"
"O" --- (for other)   # any systems in <filename> that start with the letter "o"

<scope>:           -s
"A" --- (for all systems in <filename> as defined by the -b switch eg. if user puts in "L" for the -b switch, and "A" for the s switch, then all ("A") liquor systems as defined in <filename> (as entered at the command line for the -f switch), would be processed),  
"S" --- (for systems ending in "NT001" in <filename>),
"N" --- (for systems that DONT end in "NT001" in <filename>)

<remote file>:      -n
a supplied full LOCAL path and file to the file you wish to check the existence for.

<filename>:        -f
a supplied name of a file that has a list of computers in it, 1 computer per line.
This file will be processed based on the criterion of the aforementioned switches, in order for filtering of machines in <filename>.

So, an example of user input to run the script would be:
checkfile.pl -b L -s A -n "d:\bin\testfile.txt" -f systems.txt

What the above would do is:
1) Read the systems.txt file (as defined by -f <filename>
2) Search for the business "L" - any systems that start with "L" (as defined by -b <business)
3) Grab ALL systems that start with "L" - all because of the -s A (as defined by -s <scope>)
4) Check for the existence of "d:\bin\testfile.txt" on the remote system (as defined by -n <remote file>)
5) If the file exists, write to a file called "fileexists.txt" with an entry for the computer name.
6) If the file doesnt exist, write to a file called "filenotexists.txt" with an entry for the computer name.
7) The user should be able to miss some of the parameters, thus being able to do the following:

checkfile.pl -n "d:\bin\testfile.txt" -f systems.txt

In which case, the script would read the systems.txt file, process every system in systems.txt, disregarding the -b and -s switches, and search for the existence of "d:\bin\testfile.txt" on the remote machine.

As the entry for the -n switch is inputted as local eg."d:\bin\testfile.txt", the d: has to be interpreted as a d\$
In other words, when a computer name is read from the <filename>, for example, LR2313NT001, when checking for the file existence, the path is \\\\$SystemName\\d\$\\bin\testfile.txt, if $SystemName is chosen as the variable name to set for the computers in <filename>.

I know how to do the code to connect to each machine, but the code for the multiple parameters is overwhelming, and any help appreciated.

This question will probably go over 2 questions, as I know there is a lot here.

Thanks guys.
0
Comment
Question by:Simon336697
  • 4
  • 4
8 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 19556717
You can read parameters easily with the Getopt:Long module:
http://search.cpan.org/~jv/Getopt-Long-2.36/lib/Getopt/Long.pm


my ($Business, $Scope, $RemFileName, $FileName);
GetOptions ("b=s" => \$Business,
            "s=s" => \$Scope,
            "n=s" => \$RemFileName,
            "f=s" => \$FileName) or die "Invalid options\n";

die "Invalid -b option.  Valid options are L, S, and O.\n" unless $Business =~ /^(L|S|O)$/;
die "Invalid -s option.  Valid options are A, S, and N.\n" unless $Scope =~ /^(A|S|N)$/;
0
 
LVL 39

Expert Comment

by:Adam314
ID: 19556732
what is the format of the systems.txt file?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19556855
Hi Adam (hope you are going well!).

Mate the format is as follows for systems.txt

LN5834NT001
LS9286NW001
LW2722NW131
SA1209NT001
AS2390NT001
OS7399NQ001
LV6391NW231
OQ2322NT001
OV3187NT001
AN2333NT001
SW1259NT001
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Simon336697
ID: 19558792
In the following systems.txt

LN5834NT001
LS9286NW001
LW2722NW131
SA1209NT001
AS2390NT001
OS7399NQ001
LV6391NW231
OQ2322NT001
OV3187NT001
AN2333NT001
SW1259NT001

------------------------------
The first character of each computer name is the business code.
So,
if its "L", its liquor,
if its "S", its store,
anything else is "O" for other.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19558826
Example:

checkfile.pl -b <business> -s <scope>  -n <remote file> -f <filename>

checkfile.pl -b S -s S -n "c:\test.txt" -f systems.txt

would work on finding systems starting with S, ending with NT001, testing for existence of c:\test.txt, computer system list from systems.txt

computers to process would be:

SA1209NT001
SW1259NT001

From the list mentioned in my last comment.

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 19559288
use Getopt::Long;

my ($Business, $Scope, $RemFileName, $FileName);
GetOptions ("b=s" => \$Business,
            "s=s" => \$Scope,
            "n=s" => \$RemFileName,
            "f=s" => \$FileName) or die "Invalid options\n";

die "Invalid -b option.  Valid options are L, S, and O.\n" unless (!defined($Business) or ($Business =~ /^(L|S|O)$/));
die "Invalid -s option.  Valid options are A, S, and N.\n" unless (!defined($Scope) or ($Scope =~ /^(A|S|N)$/));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTYES,">fileexists.txt") or die "Could not open exists: $!\n";
open(OUTNO,">filenotexists.txt") or die "Could not open notexists: $!\n";
while(<IN>) {
      chomp;
      if(defined($Business)){
            next unless /^$Business/;
      }
      if($Scope eq 'S') {
            next unless /NT001$/;
      }
      if($Scope eq 'N') {
            next if /NT001$/;
      }
      my $LookFor = "\\\\$_\\$RemFilePath";
      if(-e $LookFor){
            print OUTYES "$_\n";
      }
      else {
            print OUTNO "$_\n";
      }
}
close(IN);
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19569515
Adam mate thank you for this.

I havent had time to properly test this, but it looks great!
0
 
LVL 39

Expert Comment

by:Adam314
ID: 19569634
Let me know if there are any problems.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question