Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with environment variable

Status
Not open for further replies.

donny750

Programmer
Jul 13, 2006
145
0
0
FR
hello,

I've a perl script;
In this script , i use a environmental variable;
But if my variable is not defined, i prompt and i can write a value for this environnemental variables.
I've a problem;
How can i do , to export this variable and use this in another script ?

Thanks;
 
i've try this
but the export not run
Code:
#!/usr/bin/perl

my $envone;
if ( !defined( $ENV{"envone"} ) ) {
    print "Enter value for envone : ";
    $envone = <STDIN>;
    chomp ($envone);

  
  my $run = system("envone=".$envone);
  
  if ($run == 0 )
{
    print "YES\n";
}
else 
{
    print "NO!\n";
    
}   

  
  
   $run = system("export envone");
  
  if ($run == 0 )
{
    print "exporting\n";
}
 
In the line:
$run = system("export envone");
if you change that to:
$run = system("export $envone");
Would it have a better chance?
 
i think no,
because envone is the name of my environment variable and $envone is the value;
i add a new value to my variable with this :
envone = $envone
and after i export my variable :
export envone
 
is "export" a program?

- Kevin, perl coder unexceptional!
 
it's not a program it's a unix command;
If you have an environnement variable not defined or you want to change the value;
You make this :
environnementvariable = newvalue
and
export environnementvariable

and so your environnement variable has the new value and you can use it in your script
 
try changing this line:

Code:
my $run = system("envone=".$envone);

to:

Code:
my $run = system("envone=$envone");

and see if that helps.

But you can do this:

Code:
$ENV{'envone'} = $envone;

and it should work as long as the script is running.


- Kevin, perl coder unexceptional!
 
i've try this
$ENV{'envone'} = $envone;
if the script run ,it works

but if i use the variable in another script;
the variable are not defined
 
OK, well, I am not familiar with the export unix command so I do not know why it will not works like you want.

- Kevin, perl coder unexceptional!
 
Hi donny750,

In UNIX, each script runs in its own 'process space'. A parent process can 'export' environmental variables to child processes. Child processes cannot easily* 'export' environmental variables to parent processes OR to other child processes. Parent processes cannot easily* 'export' to other parent processes.
(* I say cannot easily because it requires in-depth knowledge of 'Inter-process Links' (IPL) and attending courses in 'Internals' - which is beyond me)

A way round it, is to use a common 'source' file (see unix man pages - man source) that scripts can access and obtain values for environmental variables. These could be made available to all users or groups of users, etc.

For further info, please re-post specific questions.

I hope that helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top