Operating System - HP-UX
1753774 Members
7210 Online
108799 Solutions
New Discussion юеВ

How to use program to set enviroment in the current shell?

 
jacklee
Occasional Contributor

How to use program to set enviroment in the current shell?

I want to develop a program to set enviroment of current shell. I try to use putenv function, but it can only set enviroment on current process. How can I do ? By the way, can complete this task with Java?

Any suggestion is appreciated.
2 REPLIES 2
Klaus Crusius
Trusted Contributor

Re: How to use program to set enviroment in the current shell?

The bad news for you. You cannot change the environment of the curretn process(shell) by calling a child process. That is part of the Unix philosophy, I guess.
The good news. There are some ways around.
1. Source a shell file
( #. shell_script_setting_parameters
2. Set enviroment for a child process.
(# env name=value command)
3. Write your own program simulating env
4. If you want to pass output from a child process and modify environment variables of the parent shell:
from the child process create output on stdout in the form "NAME='value';export NAME".
execute the output "eval $(child_process)"
5. Write output in form of a script to a file and source in file (see. 1.)

Excpecting some points, Klaus
There is a live before death!
Kenneth Platz
Esteemed Contributor

Re: How to use program to set enviroment in the current shell?

Jacklee,

The only thing I can think of doing would be to write a program that does something similar to the following:

#include
#include

void main() {
uid_t uid;
struct passwd *pw;
.
/* Tinker around with your environment here */
.
.
uid = getuid();
pw = getpwuid( pw );
execl( pw->pw_shell, pw->pw_shell );
}

And then exec myprogram. You won't have the SAME shell you started out with, but you'll have a shell (as defined by the /etc/passwd entry) that has all of your new environment variables defined in it.
I think, therefore I am... I think!