[comp.unix.questions] The environment of the parent

pinkas@mipos3.UUCP (Israel Pinkas) (11/19/86)

A co-worker of mine has asked me a question that I could not resolve and am
now asking the net to help me with.

He has a program which look in the environment (using getenv()) for certain
strings.  If the strings are not present, he asks the user for values.
What he would like to do is set the value of these strings so that
subsequent runs of the program will be able to find the values.  That is,
he would like to place the typed-in values in the parents environment
space.  (This program is being ported from VMS, which allows such things to
be done.)

Calls to putenv() are not the solution as programs only receive a copy of
the parent's (read shell) environment and are only able to modify their own
environment.  This does allow you to spawn a shell, which will have any
values placed in the environment, but this is not what we want.

Any pointers as to how to get at, and set, the parent's environment would
be helpful.

Note: We are running Ultrix v1.2 on a variety of Vaxen.  Info about any
Un*x-like OS would be helpful.  If there is enough response, I will
summarize on comp.unix.questions.

-Israel

-- 
----------------------------------------------------------------------
UUCP:	{amdcad,decwrl,hplabs,oliveb,pur-ee,qantel}!intelca!mipos3!pinkas
ARPA:	pinkas%mipos3.intel.com@relay.cs.net
CSNET:	pinkas%mipos3.intel.com

amos@instable.UUCP (Amos Shapir) (11/20/86)

In article <261@mipos3.UUCP> pinkas@mipos3.UUCP (Israel Pinkas) writes:
> [ how to set the parent's environment from inside a program ]

Have the program output 'setenv' or 'set...export' commands for the
required variables (use the env var SHELL to know which), and run it
as:  eval `program`
If you have a BSD system or clone, see how 'tset' is used to set
the env variable TERM.
-- 
	Amos Shapir

National Semiconductor (Israel)
6 Maskit st. P.O.B. 3007, Herzlia 46104, Israel
(01-972) 52-522261  amos%nsta@nsc
34.48'E 32.10'N

stuart@bms-at.UUCP (11/21/86)

In article <261@mipos3.UUCP>, pinkas@mipos3.UUCP (Israel Pinkas) writes:

> he would like to place the typed-in values in the parents environment

> Any pointers as to how to get at, and set, the parent's environment would
> be helpful.

In *nix, you have to *be* the parent.  Other methods involve cooperation
with the parent.

You could make your command a shell alias which would
'eval' the output of your program:

chkenv() {		# SysV sh
eval `chkenv.prog`
}

'chkenv.prog' should output something like:

VAR1=abc; VAR2=def; export VAR1 VAR2

'csh' users need their own special syntax.
-- 
Stuart D. Gathman	<..!seismo!{vrdxhq|dgis}!bms-at!stuart>

henry@utzoo.UUCP (Henry Spencer) (11/25/86)

> Any pointers as to how to get at, and set, the parent's environment would
> be helpful.

You can't.  Only the parent itself can modify the parent's environment.
You will need to either find a different mechanism for communication, or
else enlist the parent's cooperation in some manner.

It is agreed that this is a bit inelegant.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,decvax,pyramid}!utzoo!henry