469,353 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Passing parameters to a perl file from .sh file

How to pass parameter to a perl file (say myperl.pl) from a shell file (say work.sh ) ? and how to get that parameter in perl file ? { is using my $var = @_ }.

Thanks,
Pawan Kumar
Feb 9 '07 #1
4 12001
arne
315 Expert 100+
How to pass parameter to a perl file (say myperl.pl) from a shell file (say work.sh ) ? and how to get that parameter in perl file ? { is using my $var = @_ }.

Thanks,
Pawan Kumar
Don't know if I understood the question ... you want to invoke a perl prog (say perl.pl) from a shell script (say work.sh) and a pass a var from the shell script to the perl program?

What about (work.sh)
Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. VAR='whatever'
  3. perl perl.pl var
  4.  
and in perl.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. my $var = $ARGV[0];
  3. #$var should be 'whatever' now
  4.  
HTH
Feb 9 '07 #2
jkr
3
In the above sample it should read:
perl perl.pl $VAR

---------

you can do the following:

$ export VAR='whatever'

and run:
myperl.pl
[HTML]#!/usr/bin/perl -w

defined($arg=shift(@ARGV)) && print "arg: $arg\n";
print "var: $ENV{'VAR'}\n";[/HTML]
will print:
var: whatever

./myperl.pl argument
will print:
arg: argument
var: whatever
Feb 9 '07 #3
Thanks arne and jkr.

arne, You correctly got the question. I was missing perl before myperl.pl. Since in my case, .sh file will pass paramenter at every 5 minute (Automated script), I feel first method ( of arne) is more relevent ( every time just pass the parameter as argument). thank you.

jkr, from your answer, I learned a new way of doing the same thing in a different way. thanks.

Regards,
Pawan Kumar
Feb 11 '07 #4
@pawanrpandey

If the word perl in front of your command line does the trick, you probably do not have a "she-bang" line in your perl script.

If the first line of your perl script is
Expand|Select|Wrap|Line Numbers
  1. #!<path>/perl
where <path> is the path to your perl interpreter, and if the script is executable (chmod +x <script>), you do not need to put the word perl in front of your command line. You can call the script directly, instead.

Greetz, Doc
Feb 12 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by google_nospam | last post: by
1 post views Thread by Romuald Favre | last post: by
7 posts views Thread by Harolds | last post: by
reply views Thread by Xah Lee | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.