perl. How to set persistent environment variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl. How to set persistent environment variables
# 1  
Old 05-25-2010
Question perl. How to set persistent environment variables

I have test.pl scrit with these few lines.

Code:
#!/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.

Code:
[]-> . 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

SmilieSmilieSmilie

Last edited by Scott; 05-25-2010 at 02:55 PM.. Reason: Code tags, please...
# 2  
Old 05-25-2010
Hi.

Maybe you should use
Code:
. ./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.
# 3  
Old 05-25-2010
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
# 4  
Old 05-25-2010
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.
# 5  
Old 05-25-2010
Quote:
Originally Posted by MKNENI
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
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!)
Code:
$ 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

Quote:
Originally Posted by SFNYC
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.
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 Smilie

Last edited by Scott; 05-25-2010 at 03:29 PM.. Reason: Cut and pasted error
# 6  
Old 05-25-2010
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.
# 7  
Old 05-25-2010
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.

Code:
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.

Last edited by Corona688; 05-25-2010 at 03:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Trying to figure out how the environment variables are being set

I just started a new job and I've been tasked with cleaning up the files that set up all the environment variables. The system works as is. What happens is: 1. You log in to the server. 2. You call a file that sets a bunch of environment variables and that displays a list of all the databases... (4 Replies)
Discussion started by: Keyeh
4 Replies

2. Shell Programming and Scripting

Need to SET Environment variables

Hi Could you please tell me how to set environment variables in Unix ksh. And how can acess those varibles in shell scripts ( Please give the code with an example) For my scenario. We have written number of shell scripts with hard coded username and password. But if we want to... (1 Reply)
Discussion started by: shyamu544
1 Replies

3. SCO

Help finding where certain environment variables are set

i have two machines that should be identical but on one system there are some oracle environment (ORACLE_SID, ORACLE_HOME, etc...) variables that are not being set for the users. I am trying to find where those environment variables are being set on the system which is working properly. All... (5 Replies)
Discussion started by: kuliksco
5 Replies

4. Shell Programming and Scripting

Set environment

Hi, I can run shell script from the command line using $ . set If the run the script inside perl script using $var = system("set"); print $var; This prints 0. This command sets up the environment from command line. But when used inside the shell script or perl script it... (2 Replies)
Discussion started by: sandy1028
2 Replies

5. Solaris

set environment variable?

I am working with solaris 9 sunBlade150 Box. I Installed a program, need to set the environment variable so that when the executable is entered,it finds the path to the executable. The documentation for the software says: Set the appropriate environment variable: Connect to server failed;... (8 Replies)
Discussion started by: smartgupta
8 Replies

6. Shell Programming and Scripting

Environment variables in Perl

I don't use perl very often, and am stuck on how to create a perl variable from a literal and an environment variable; eg $FILE='/u/output'+env($UNIQUE_ID); Also how does the Apache web server create the UNIQUE_ID environment variable, and will an 8 character substring from 3 to 10 incl, in... (2 Replies)
Discussion started by: jgt
2 Replies

7. Linux

How do i set environment variable

Hi, I am quite new to Linux. And I have doubt how to set new environment variable with value to a C executable. Let say I have a environment variable $Hack ; I would like to load a value for this variable; so that when the C executable is executed, the $Hack would set the variable value. ... (4 Replies)
Discussion started by: ahjiefreak
4 Replies

8. UNIX for Advanced & Expert Users

by using c++ how to set environment variables in unix

hi, I am writing c++ code in unix operating system.In that i need to set the environment variable in unix. suppose previously i have environment variable like path="something" now i need to change the path value to some othervalue . so that some other program will access that path value... (1 Reply)
Discussion started by: sada@123
1 Replies

9. UNIX for Advanced & Expert Users

set environment variable?

Installed a program, need to set the system up so that when the executable is entered, it finds the path to the executable. In Windows, set under system properties, advanced, environmental variables. How do I do this with Unix? Specifically using Solaris 9. I have tried: env... (3 Replies)
Discussion started by: kohoutek
3 Replies

10. UNIX for Dummies Questions & Answers

how to set up linux environment variables?

Hi I'm using Linux, in the directory /root/my there is a.out. but when I try to run it , the shell indicate "bash:a.out: command not found" but I AM working in this directory. if I use "./a.out" , it works perfectly. can any body tell me how to do a permanent set up so that I can use... (5 Replies)
Discussion started by: dell9
5 Replies
Login or Register to Ask a Question