Unix Shell - Is there an "undo" for export at the end of a shell script?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > Is there an "undo" for export at the end of a shell script?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Is there an "undo" for export at the end of a shell script?
Ulf Meinhardt

2005-02-21, 5:59 pm

Assume I change an environment variable at the beginning of a shell script e.g.

export BLA=myval:$BLA

Notice that $BLA already exists and has an unknown content. How do I switch back
to the original value at the end of the shell execution?

At a first glance I could store the initial value in a local variable:

savebla=$BLA
export BLA=myval:$BLA
.....
....other manipulations of $BLA
.....
(*1)
.....
export BLA=$savebla
exit


But what happens if the shell crashes at (*1) ?
The env variable has no chance to get back its original value.

Ulf

Barry Margolin

2005-02-22, 5:52 pm

In article <cv32mg$js5$05$1@news.t-online.com>,
ulf2m@email.com (Ulf Meinhardt) wrote:

> Assume I change an environment variable at the beginning of a shell script
> e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch
> back
> to the original value at the end of the shell execution?


Why do you need to do this? Nothing you do in the shell script has any
effect on the parent process's environment.

Export makes variables get inherited by *child* processes that are
spawned from the script, they aren't inherited the other way.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Pat Welch

2005-02-23, 6:01 pm


Ulf Meinhardt wrote:

> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.
>
> Ulf
>


No problem as the export *only* applies to the *current* script or other
scripts launched from the *current* script.

if you write a simple script to change and export a vari, like:

ATEST=1234
export ATEST
echo $ATEST

Then do, at the console prompt AFTER running the above:

echo $ATEST

you'll see its either blank or the original value.

--
----------------------------------------------------
Pat Welch, UBB Computer Services, a WCS Affiliate
SCO Authorized Partner
Unix/Linux/Windows/Hardware Sales/Support
(209) 745-1401 Cell: (209) 251-9120
E-mail: patubb@inreach.com
----------------------------------------------------
nospam@geniegate.com

2005-02-24, 5:58 pm

In: <cv32mg$js5$05$1@news.t-online.com>, ulf2m@email.com (Ulf Meinhardt) wrote:
>savebla=$BLA
>export BLA=myval:$BLA
>.....
>....other manipulations of $BLA
>.....
>(*1)
>.....
>export BLA=$savebla
>exit
>
>
>But what happens if the shell crashes at (*1) ?
>The env variable has no chance to get back its original value.


Thats one of the beautiful things about unix, each process gets it's own
inherited environment. No worries about the above problem.

You can set any environment variable you want, change to any directory you
want, the environment won't be carried back to the parent process. (Unless
you .source the script)

This can be an issue if you ever need to tell the parent what you're up to,
but 99.9% of the time, it's ideal. (If you had to talk to the parent, you'd
probably echo the stuff to a /tmp file and then source it in on the parent side)

Imagine running a process (from /tmp/foo) that changes directories to, say /etc,
now you, believing you're still in "/tmp/foo" type rm *

Oops.

Thats not a problem with the unix model.

As an experiment, try writing a shell script that replaces the 'cd' command. :-)

Jamie
--
http://www.geniegate.com Custom web programming
guhzo_42@lnubb.pbz (rot13) User Management Solutions
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com