Passing Value from Shell to Perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing Value from Shell to Perl

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-05-2007
hcbhatt hcbhatt is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 5 November 2007, 12:41 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Passing Value from Shell to Perl

Hi All,

I am calling a perl program in my shell script as follows.

MY_IN_FILE=ABC.dat
MY_OUT_FILE=XYZ.dat
MY_VARIABLE="SomeValue"

perl mycode.pl $MY_IN_FILE > $MY_OUT_FILE

Question:-
Now I want to pass value of $MY_VARIABLE from script to perl... How do I do that? Can someone please help, I'm new to perl and scripting !!

Thanks.
HCB
Sponsored Links
    #2  
Old 11-05-2007
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
The four primary ways of inputting some to a program (no matter the language the program is written in)

(a) command line argument, like your file argument

(b) environment variables, typically need to export from a shell script

(c) as stdin, basically the input stream to a program

(d) as a generic file, where the program looks for well-known files.
Sponsored Links
    #3  
Old 11-05-2007
gus2000 gus2000 is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 17 November 2011, 6:36 PM EST
Posts: 160
Thanks: 0
Thanked 1 Time in 1 Post
accessing environment variables from perl

Environment variables in perl are accessed via the $ENV list (hashed array):


Code:
# typeset -x MY_VARIABLE="SomeValue"
# perl -e 'print $ENV{"MY_VARIABLE"}'
SomeValue

Of course, you could write the whole script in perl, but...baby steps.

Note that the variable(s) must be exported. I used "typeset -x" instead of "export" since I regularly typeset all the variables used in scripts. It's a good habit to adopt, since variables can be inherited if they are not typeset.

Wanna see all your scripts blow up? Try "typeset -r i" at the prompt before running them and watch what happens! This makes "$i" read only...unless the script typesets it.
    #4  
Old 11-05-2007
hcbhatt hcbhatt is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 5 November 2007, 12:41 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by porter View Post
The four primary ways of inputting some to a program (no matter the language the program is written in)

(a) command line argument, like your file argument

(b) environment variables, typically need to export from a shell script

(c) as stdin, basically the input stream to a program

(d) as a generic file, where the program looks for well-known files.
I have tried
export $MY_VARIABLE

and then reading $MY_VARIABLE in perl program. I didnt get any value!

I tried as command line argument, however since I already have one file as input paramenter, it is interpreting another also as file input and I get a message that $MY_VARIABLE file does not exists.

Can you please show me syntax of passing file as one parameter and value as another parameter? or any other way?
Sponsored Links
    #5  
Old 11-05-2007
hcbhatt hcbhatt is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 5 November 2007, 12:41 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by gus2000 View Post
Environment variables in perl are accessed via the $ENV list (hashed array):


Code:
# typeset -x MY_VARIABLE="SomeValue"
# perl -e 'print $ENV{"MY_VARIABLE"}'
SomeValue

Of course, you could write the whole script in perl, but...baby steps.

Note that the variable(s) must be exported. I used "typeset -x" instead of "export" since I regularly typeset all the variables used in scripts. It's a good habit to adopt, since variables can be inherited if they are not typeset.

Wanna see all your scripts blow up? Try "typeset -r i" at the prompt before running them and watch what happens! This makes "$i" read only...unless the script typesets it.
Thanks! It worked.

I'll read further on typeset, as you mention it to be good coding habit ... and also especially after reading usage of typeset -r i

Thanks again to all who replied !
Sponsored Links
    #6  
Old 11-05-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 12 October 2014, 12:11 PM EDT
Posts: 149
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by hcbhatt View Post
I have tried
export $MY_VARIABLE

and then reading $MY_VARIABLE in perl program. I didnt get any value!
should be

Code:
export MY_VARIABLE

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing perl variable to shell command Anjan1 Shell Programming and Scripting 2 03-27-2011 09:32 PM
Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl akil Shell Programming and Scripting 7 07-17-2009 09:09 AM
Passing a file handler and an array from Perl to Shell Script pinkgladiator Shell Programming and Scripting 5 11-17-2008 10:00 PM
Need help passing variables in shell script to perl one-liner Xek Shell Programming and Scripting 1 01-15-2008 02:12 PM
Passing variable to perl TheCrunge Shell Programming and Scripting 2 06-06-2006 04:43 PM



All times are GMT -4. The time now is 06:25 PM.