LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 07:37 PM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

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, 08:17 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can save your IDs in a file, then pass the file to the perl script?
 
Old 01-13-2008, 09:36 PM   #3
noir911
Member
 
Registered: Apr 2004
Posts: 682

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, 10:01 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 10:12 PM   #5
noir911
Member
 
Registered: Apr 2004
Posts: 682

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, 10: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, 12:02 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 06:55 AM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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 06:58 AM.
 
Old 01-15-2008, 04:59 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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 05:00 AM.
 
  


Reply



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 01:51 PM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 09:51 PM
client server raw socket without using command line argument edens_2001 Linux - Networking 1 03-13-2007 07:14 AM
Finding the last command line argument (bash) pete1234 Programming 20 10-30-2006 10:20 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration