Operating System - Linux
1751807 Members
3473 Online
108781 Solutions
New Discussion юеВ

Re: Setting Environment variable in UNIX Shell

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: Setting Environment variable in UNIX Shell

Hi:

You can't get where you want directly as stated by many. Another variation is to let your Perl script do whatever its objective is and create a file of the variables you want exported. Then, at will, source (read) them into your current environment:

# cat some.pl
#!/usr/bin/perl
open( FH, ">", 'env.sh' ) or die;
print FH "HAI=hai\n";
print FH "HELLO=Hello\n";

# ./some.pl
# . ./env.sh
echo ${HELLO}
Hello

Regards!

...JRF...

CA1490051
Frequent Advisor

Re: Setting Environment variable in UNIX Shell

Hi JRF,

Yes i am also thinking of the same solution but only thing is i have planned to execute both the perl script and the generated shell script through another shell script.

Ex -
!#/usr/bin/sh
Some.pl
chmod +x Env.sh
source Env.sh

Where Env.sh is the generated file.

But if i do this i m getting the error source not found. Can you correct me if i am executing the shell script in wrong way please
thanks and regards
Vikram

James R. Ferguson
Acclaimed Contributor

Re: Setting Environment variable in UNIX Shell

Hi (again) Vikram:

> Yes i am also thinking of the same solution but only thing is i have planned to execute both the perl script and the generated shell script through another shell script.

Fine, do something like this:

#!/usr/bin/sh
echo "running Perl next"
/home/vikram/perlthing #...outputs /home/vikram/env.sh
. /home/vikram/env.sh #...sources env.sh
echo "Now I can say ${HELLO}"
...

Regards!

...JRF...
CA1490051
Frequent Advisor

Re: Setting Environment variable in UNIX Shell

Hi all,


thank you all for the reply

I got the solution i.e,

execute the perl script through a shell script and write all the env variables into a file and execute this file as source i.e. , by . ./Env.sh and also execute the shell script that is executing the Perl script also as source .
James R. Ferguson
Acclaimed Contributor

Re: Setting Environment variable in UNIX Shell

Hi (again) Vikram:

By the way:

You don't need to turn the executable bit on to source (read) a file as you showed in your example, above.

Also: If you are using the HP-UX POSIX shell (the standard one) you need to use the dot-space-filename notation to source. If you are using the 'bash' shell, your 'source' notation is fine. NEVER change 'root's shell from '/sbin/sh' to anything else or risk having a system that will not startup after a boot!!!

Regards!

...JRF...
CA1490051
Frequent Advisor

Re: Setting Environment variable in UNIX Shell

Thank you all for your valuable suggestions
Solution i found i have already shown in my reply
thanks and regards
Vikram