Solved

passing args in UNIX to Perl script?

Posted on 1999-01-07
5
265 Views
Last Modified: 2010-03-05
How do I pass arguments to a perl script in the UNIX shell?
0
Comment
Question by:tpchai
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:blu
ID: 1207377
The args to a perl script are stored in the array, @ARGV.

cat args.pl

#!/bin/perl

$i=0;
print "num of args=$#ARGV\n";
foreach (0..$#ARGV) {
        print "$i=$ARGV[$i]\n";
        $i++;
}

args.pl arg0 arg1 arg2 arg3
num of args=3
0=arg0
1=arg1
2=arg2
3=arg3

0
 

Author Comment

by:tpchai
ID: 1207378
Sorry, should have been a little more clear in my question. Say my perl script expects 2 name/value pairs, eg. on a web form <input type=text name=sex> <input type=text name=age>, how  do I simulate submiting this form with sex=male age=20 in the UNIX shell.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207379
GET or POST?

You can pass them in environment variables, e.g.
QUERY_STRING="sex=male&age=2";
export QUERY_STRING
or, if you
use CGI.pm;
you can pass also them on the command line
args.pl sex=male age=2
0
 

Author Comment

by:tpchai
ID: 1207380
Ozo, thanks, perhaps you can submit that as an answer. What diff does it make whether it's GET or POST?
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 1207381
when $ENV{REQUEST_METHOD} eq 'GET', the data is passed in $ENV{QUERY_STRING}
when $ENV{REQUEST_METHOD} eq 'POST', $ENV{CONTENT_LENGTH} is the length of the data sent via <STDIN>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 165
Check only file date not subfolder date before getting folder path 3 68
Perl modules on linux ec2 3 96
quoting a comma separated list 20 82
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now