Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Call shell script variables in perl

by k_manimuthu (Monk)
on Mar 21, 2011 at 09:46 UTC ( #894450=perlquestion: print w/replies, xml ) Need Help??
k_manimuthu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a perl file and .sh file. the perl file call the sh file. I want to use the shell script variable into the perl file. Is there any possible way to access the shell variable in to a perl file in run time. Below I show my sample perl file and the .sh file.


perl Script

#!/usr/bin/perl # File Name : # Initial level stuffs # Some stuffs system ("sh mypath"); # Any possible way to use the shell script variable here print "\n$MY_LIB_PATH";

Shell Script

#!/bin/sh # File Name : VAR1="value 1" VARn="value n" MY_HOME=$1 MY_LIB_PATH=$ORACLE_HOME/$MY_HOME MY_LIB_PATH=$ORACLE_HOME/$MY_HOME/$VAR1 export $MY_LIB_PATH; echo $MY_LIB_PATH

Replies are listed 'Best First'.
Re: Call shell script variables in perl
by Eliya (Vicar) on Mar 21, 2011 at 10:41 UTC

    system runs a subprocess, and because a subprocess gets its own copy of the environment, you cannot set/modify an environment variable from within a subprocess such that it will be available to the parent process (your Perl script here).  In other words, the new subprocess' environment is gone as soon as the process has ended, i.e. when system returns.  Exporting won't help.

    But you could print the variable's value to stdout in the shell script, and capture it in the Perl script (e.g. via backticks).

      Thanks for your information. I am showing (STDOUT) more information to the end user. I want to use the particualar variable only in my perl program. I will get the output as you metioned method and do regex stuff to to get the result. Thanks

Re: Call shell script variables in perl
by moritz (Cardinal) on Mar 21, 2011 at 09:51 UTC
    See %ENV. But it only works if you do the exporting correct: export MY_LIB_PATH; (no $ on the variable name).

    Update: d'oh, just read Eliya's reply, and realized that I haven't read the scripts carefully enough.

Re: Call shell script variables in perl
by Corion (Pope) on Mar 21, 2011 at 10:59 UTC
Re: Call shell script variables in perl
by Anonymous Monk on Mar 21, 2011 at 13:14 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://894450]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2018-06-21 14:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.