perl. How to set persistent environment variables

I have test.pl scrit with these few lines.

#!/usr/bin/perl
$ENV{'ORACLE_SID'} = "D3771";
$ENV{'ORACLE_HOME'} = "/oracle/product/10.2.0/db_1";

When I try . test.pl it throws an error. When I try test.pl, it doesn't reaing the variables I set in the script.

[]-> . test.pl
ksh: {ORACLE_SID}:  not found
ksh: {ORACLE_HOME}:  not found

[]-> test.pl

[]-> echo $ORACLE_SID

[]->

Is there a way to get around this issue ?

Thanks for you help

:confused::confused::confused:

Hi.

Maybe you should use

. ./test.pl

(assuming it's executable, otherwise make it so with chmod)

Reason is most likely that the current directory (.) is not in your PATH - generally a good thing!

Out of interest, why are you trying to set environment variables using Perl, in this way?

Unless your current (could be your default?) shell is Perl (which is unlikely), this is bound to fail.

No luck. I tried with ./ and specifying full path. Getting the same error message.

I need to do a lot of string manipulation before setting these 2 environment variables. I feel more comfortable with perl than shell scripting. That is the reason behind using perl here.

Thanks

Your PERL program inherits its environment from the SHELL that spawned it, changing environment variables will only last for the duration of the PERL program.

At the shell prompt, if you echo $ORACLE_SID and $ORACLE_HOME, do you see any values? If you want to set these two environment variables, perhaps you should set them from your profile instead.

Great, but you can't source a Perl script into a Bash / Ksh, etc. environment.

Perhaps an alternative, if you're more comfortable in Perl is to do the string manipulation there, and then print the values. Then, the shell can be used to set them.

(excuse my Perl!)

$ cat Perl1
#!/usr/bin/perl
$ENV{'ORACLE_SID'} = "D3771";
$ENV{'ORACLE_HOME'} = "/oracle/product/10.2.0/db_1";

printf( "%s %s\n", $ENV{'ORACLE_SID'}, $ENV{'ORACLE_HOME'} );


$ cat Test1
#!/usr/bin/ksh
./Perl1 | read ORACLE_SID ORACLE_HOME

echo $ORACLE_SID
echo $ORACLE_HOME


$ ./Test1
D3771
/oracle/product/10.2.0/db_1

If you have one Oracle instance (and no global listener to access a remote one) locally then that's fine.

In any case, I always believe that the easiest way to set up an Oracle environment is by using the Oracle-provided oraenv script :slight_smile:

echo $ORACLE_SID and $ORACLE_HOME is showing blanks.

I can't set this in the .profile because I keep executing the script by passing a parameter and this perl script should set the env variables based on the parameter.

It has to be run inside your current shell in order to successfully set environment variables. This means your perl script will never work -- even if perl runs, your perl interpreter will have its own separate environment and the changes won't happen in your own shell.

Fortunately it's extremely easy to convert this script into a generic shell script.

export ORACLE_SID="D3771"
export ORACLE_HOME="/oracle/product/10.2.0/db_1"

You should be able to source this file fine. It needs no #!/bin/ksh line and does not need to be set executable.

It is very easy, but don't overlook the O/P's statement about string manipulation (of which we know nothing :))

That changes the problem quite a bit.

You will need to have the script execute the necessary program inside it then, so it can export things the way you want them, then launch the program with the updated environment.

#!/bin/ksh

export ORACLE_SID="D3771"
export ORACLE_HOME="/oracle/product/10.2.0/db_1"

exec my_program

Or in perl:

#!/usr/bin/perl

export_environment_as_I_please;

system("program");

do you need to run ur script only with 1 setting. or do u need to change both the variables depending upon some parameters.

Thanks you guys. Looks like its lot of pain using perl to set evn varialble.

I switched to shell script. That is working good.

Thanks again to everybody.

:b::b: