[comp.unix.wizards] Script to set parent environment

mvp@v7fs1.UUCP (arf) (01/18/89)

Some time ago I asked about how a csh script could change
global environment variables.  Thanks to everyone who responded.

Here's a summary:

You can't do it, and this question should be added to the 
"List of Most Frequently Asked Questions".

At least, there's no official way, because to do that requires a
child to change its parent's environment, and that's a no-no.  A
couple of messages implied that it might be possible, if one were
to play some "Real Programmer" type games.  Many replies also
said that if I wanted to call a script to set some environment
variables, I should either "source" the script ( . in sh) or
create an alias.  (I had ruled out sourcing the script because
you can't pass arguments to a sourced script.)

chris@mimsy.UUCP (Chris Torek) suggested a script which
generates and then prints the desired setenv command on standard
output, and an alias

>	% alias edit-env 'eval `~/bin/edit-env`'

to make the current shell do the actual setenv.  This works
fine, but doesn't work for the specific problem I was trying to
solve -- a script which saves its argument list, and if called
with no arguments, uses the old argument list.  (Perhaps I
should have specified what I was trying to do more exactly.)

The way I finally kludged it was more along the lines of

if $#argv > 0 then
	echo $* >~/.last-parms
endif
program-call `cat ~/.last-parms`

-- 
Mike Van Pelt                                 "Hey, hey, ho ho,
Video 7                                       Western culture's got to go."
...ames!vsi1!v7fs1!mvp                        Stanford students and faculty.

djk@munnari.oz (David James Keegel) (01/22/89)

in article <186@v7fs1.UUCP>, mvp@v7fs1.UUCP (arf) says:
] 
] Some time ago I asked about how a csh script could change
] global environment variables.  Thanks to everyone who responded.
] ...
] 				[Chris Torek's suggestion] works
] fine, but doesn't work for the specific problem I was trying to
] solve -- a script which saves its argument list, and if called
] with no arguments, uses the old argument list.  (Perhaps I
] should have specified what I was trying to do more exactly.)

Yes, you should. A general solution for this is hard, a specific one
is reasonably easy.

] The way I finally kludged it was more along the lines of
] 
] if $#argv > 0 then
] 	echo $* >~/.last-parms
] endif
] program-call `cat ~/.last-parms`

I deduce from this that you are using C-shell.

Well, my .cshrc contains the following:

alias   to    'set to=\!^; \to' \!^ "'\!:2*'"
alias   ot    '\to "$to"' "'\!*'" 

There is a horrible confusion of quotes and backslashes here, but the point
is that when I run `to' (usage: "to user message ..."; like "echo message |
write user"), it saves the user name in the variable $to, and I can use
"ot" to recover it. So these are equivalent:

	% to fred hi there		% to fred hi there
	% to fred how are you		% ot how are you

If you don't like the waste of file space from the shell script and
~/.last-parms, and you can be bothered, you feel free to adapt this idea.

-- 

                        David Keegel    (djk@munnari.oz)
   "Flattery will get you nowhere, unless someone else does it to you"