LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-13-2008, 08:37 PM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
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.
 
Old 01-13-2008, 09:17 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you can save your IDs in a file, then pass the file to the perl script?
 
Old 01-13-2008, 10:36 PM   #3
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-13-2008, 11:01 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by noir911 View Post
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
 
Old 01-13-2008, 11:12 PM   #5
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ghostdog74 View Post
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?
 
Old 01-13-2008, 11:46 PM   #6
Valdis Grinbergs
Member
 
Registered: Dec 2005
Distribution: Debian
Posts: 30

Rep: Reputation: 25
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.
 
Old 01-14-2008, 01:02 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Valdis Grinbergs View Post
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 $_ ;
}
...
 
Old 01-14-2008, 07:55 AM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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.

Last edited by Telemachos; 01-14-2008 at 07:58 AM.
 
Old 01-15-2008, 05:59 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
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;
}

Last edited by bigearsbilly; 01-15-2008 at 06:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 02:51 PM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 10:51 PM
client server raw socket without using command line argument edens_2001 Linux - Networking 1 03-13-2007 08:14 AM
Finding the last command line argument (bash) pete1234 Programming 20 10-30-2006 11:20 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 05:35 PM


All times are GMT -5. The time now is 08:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration