LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl command line argument (https://www.linuxquestions.org/questions/programming-9/perl-command-line-argument-613361/)

noir911 01-13-2008 07:37 PM

Perl command line argument
 
I have a Perl script that takes each Staff ID as command line argument and
print some info. I would like to do this for 500 staff members - is there any was I could feed a list of ID to the script and ask it to print it? Here's the script (relevant parts only) -

#!/usr/bin/perl

use Net::LDAP;
use DBI;

die "Please give Staff ID as argument" if $#ARGV != 0;
$staffname = $ARGV[0];

Thanks for any help.

ghostdog74 01-13-2008 08:17 PM

you can save your IDs in a file, then pass the file to the perl script?

noir911 01-13-2008 09:36 PM

Thanks for your help.

I tried -

./staff.pl < file.txt

and ./staff.pl file.txt

but that didn't work.

Is there any other way I can feed input from a file to the Perl script?

Thanks, again.

ghostdog74 01-13-2008 10:01 PM

Quote:

Originally Posted by noir911 (Post 3021530)
Thanks for your help.

I tried -

./staff.pl < file.txt

and ./staff.pl file.txt

but that didn't work.

Is there any other way I can feed input from a file to the Perl script?

Thanks, again.

what didn't work ? you should show all code and error messages you have.
try these:
eg
Code:

$infile = shift;
or just
Code:

$infile = $ARGV[0];
then open the file in the perl script. see a random example here

noir911 01-13-2008 10:12 PM

Quote:

Originally Posted by ghostdog74 (Post 3021550)
what didn't work ? you should show all code and error messages you have.

Sorry. I get this error: Please give Staff ID as argument ./staff.pl line 6.

I don't want to change the Perl script. Is there any way I could do it from the shell?

Valdis Grinbergs 01-13-2008 10:46 PM

Your perl program says:

die "Please give Staff ID as argument" if $#ARGV != 0;

But this makes the program stop at line 6 if the number of arguments passed to the perl script is not equal to zero ($#ARGV != 0). I think you really want:

die "Please give Staff ID as argument" if $#ARGV = 0;

Then you can call the perl script for a set of staff ids from bash with:

for item in `cat staffids.txt`; do perl perlprogram.pl $item; done

where your list of staff ids is in file staffids.txt
and perlprogram.pl is the name of your perl program.

ghostdog74 01-14-2008 12:02 AM

Quote:

Originally Posted by Valdis Grinbergs (Post 3021575)
Then you can call the perl script for a set of staff ids from bash with:

for item in `cat staffids.txt`; do perl perlprogram.pl $item; done

where your list of staff ids is in file staffids.txt
and perlprogram.pl is the name of your perl program.

this method will call the perl program for every ids inside the file. If there are many ids, then there are many calls to the perl program. Should be easier just to open the file from inside Perl script.
Code:

...
open(FILE,"<$infile") or die "Cannot open : $!";
while ( <FILE> ) {
 # do something with $_ ;
}
...


Telemachos 01-14-2008 06:55 AM

Ghostdog sounds right to me. You can tell Perl on the command line where to get the file with names saved in it. Here's a variant on the same idea:
Code:

#!/usr/bin/perl
use warnings;
use strict;

die "Usage: specify file with ids on command line.\n" unless @ARGV;

while (<>) {
    chomp;
    print "One id is [$_].\n";
    ## Substitute whatever you really want to do
    ## with the ids here instead of the two lines
    ## above.
}

If you have a file (say id_file) with ids in it, one per line. You could run this script and get something like the following output:
Code:

./process_ids id_file
One id is [sarpedon].
One id is [telemachus].
One id is [odysseus].
One id is [penelope].

You can also hardcode the filename into the script as Ghostdog did. I like this way, because then you can change it as needed more quickly. If the id file changes name, or if you have multiple id files, you just enter the new name or the multiple names on the command line. Perl will automatically process through whatever files you enter on the command line in order, one after the other.

bigearsbilly 01-15-2008 04:59 AM

you can do this, which reads through the command line
as files to open, or pipe a file into the script.

It will timeout after 1 second if no arguments or input is forthcoming...

Code:

#!/usr/bin/perl


$SIG{ALRM} = sub { die "need filename[s] or input \n";};


alarm 1;

foreach (<ARGV>) {
  alarm 0;
  print;
}



All times are GMT -5. The time now is 07:11 AM.