Operating System - HP-UX
1824169 Members
3255 Online
109669 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
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.
9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor
Solution

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

You can't do this directly since you would be changing the environment of a child process; as soon as you exit the parent knows nothing about your new environment. But there is a way to do this:
1) In your C program call putenv() to set any needed variables. Make sure that you either declare either global or local static variables
(or do not exit the function invoking the putenv call). You want the areas pointed to by your putenv() to still be defined.
2) From the same C program do a system("/usr/bin/sh");

Regards, Clay
If it ain't broke, I can fix that.
Mike Lacey
Occasional Advisor

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

There is a way to do this actually, using the dot operator.

Create a small shell script that makes the changes to the correct variables, something like this perhaps:

export MY_PATH=~/bin

Name this shell script ~/vars.sh and then, from a shell prompt, type:

. ~/vars.sh

This will run the shell script ~/vars.sh in your *current* shell. Any changes made to the environment in that script will still be visible when ~/vars.sh has completed.
Is that a haiku? I never could get the hang of writing those things.
Edgar Matzinger
Advisor

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

Hi Jack(?),

the other thing you can is, to develop a program that outputs (on stdout) the variables you want to set, and their values. And feed that output to "eval". A small example (bourne or korn shell based - I'm assuming your not using (and will never use) a C-shell):

-----------------------------8<---------------
/* setvar.c */
#include

main (int argc, char **argv) {
puts ("VAR=\"123\"\n");
}
-----------------------------8<---------------
Compile this program, and run it like this:

$ eval `./setvar`, or eval $(./setvar)

HTH, cu l8r, Edgar.
jp
Frequent Advisor

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

Yes, but.....
We want to have a central control program at log on, which looks at user name, checks what he is allowed to run, sets up some environment vars for programs, and run programs from a menu. None of the users are going to see the op sys. How do I get the system set up properly from withing this program? Cant be done readily that I can see.
How does the rest of the world do it? It must be a common requirement surely......for an OPEN system, it sure is hard work.
jp
A. Clay Stephenson
Acclaimed Contributor

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

jp,

Your requirements are trivially easy. All you have to do is to make your menu executable the user's "shell" in the passwd entry. This executable then does a putenv() to build up as complex as environment as you wish and this each menu selection is a system() command -- this forks() and exec()'s a child process that inherits the environemt. This is classic menu coding.
If it ain't broke, I can fix that.
jp
Frequent Advisor

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

Clay, Why is it that I cant see my own putenv()'s on exit from menu? I have to set up all the variables before I know what the user is going to run. So where do these putenv() objects live? why cant I see them?
I understand making the menu the user's startup program to keep them out of the OS level. While I am testing it though, I need to be able to see what's going on. Almost anything can be done from the shell level, but the users are not going to have access, so I have to do it all from inside the menu.
jp...still struggling
A. Clay Stephenson
Acclaimed Contributor

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

Because when you exit from a process, you are back in the parent process. A parent is never allowed to inherit the environment of a parent. You really need to drop the model of "returning to the OS level" and instead focus on parent process and child processes. For seem reason, you seem to be determined to make the parent inherit from the child; this is UNIX; it just ain't gonna happen.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

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

I suppose I should make clear what is going on; every process has three arguments:

int main(int argc, char *argv[], **envp)

argc = number of arguments in command line
argv = the arguments, e.g. -n 5, argv[0] is a special case; it is the name of the process itself
**envp is an array of environment variables; e.g *envp[1]="HOME=/home/mydir"; the end of this array is signaled by a NULL pointer. Putenv() modifies this array as does the export command in the shell. A COPY of this array is passed to each child process; that is why modifying the child's copy has absolutely no effect upon the parent's version of envp. This is intentional and by design; UNIX ain't MPE.

If it ain't broke, I can fix that.
jp
Frequent Advisor

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

Clay,
See my notes in thread-52696. Careful reading of man putenv...the envp ptrs , if they contain putenv data, can contain pointers to a parent process' data stack!!! surely this is breach of etiquette, if not security??
jp