| Author |
Message |
Toni Erdmann *nix forums beginner
Joined: 22 Mar 2005
Posts: 22
|
Posted: Fri Feb 18, 2005 6:49 pm Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
Ulf Meinhardt schrieb:
| Quote: | 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
|
Don't worry at all!
The value of the modified variable will not be exported to the
calling process (shell), obly to programms called by your script
Except: if you call your shell script like this
.. myscript # read: dot blank myscript
The all variables (with their new/modified values) will be exported.
Toni |
|
| Back to top |
|
 |
Chuck Dillon *nix forums beginner
Joined: 23 Feb 2005
Posts: 16
|
Posted: Fri Feb 18, 2005 1:23 pm Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
Ulf Meinhardt wrote:
| Quote: | 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?
snip
....
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.
|
The environment is per process and (for all practical purposes) only
visible to that process. A copy is passed to its children. Once the
process crashes or exits it no longer has an environment. So you don't
need to do this.
-- ced
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc. |
|
| Back to top |
|
 |
Jan Kandziora *nix forums beginner
Joined: 20 Feb 2005
Posts: 41
|
Posted: Fri Feb 18, 2005 10:49 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
Ulf Meinhardt schrieb:
| Quote: | 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.
Environment variables are specific to a process and can be "exported" to the |
process' childs. There is no way a process can change the environment of
the process *it was called by*, so it is *not necessary* to "unset" any
environment variables before exit. That naturally also applies to processes
which run wild and break right in the middle.
Please read a book about shell scripting to learn more.
Note: This does not apply to shell scripts executed on behalf of the current
shell process via the "." shell builtin.
--
Jan |
|
| Back to top |
|
 |
Doug Laidlaw *nix forums Guru
Joined: 20 Feb 2005
Posts: 336
|
Posted: Fri Feb 18, 2005 10:29 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
Ulf Meinhardt wrote:
| Quote: | 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
|
I stand to be corrected, but it is my understanding that variables set by a
script are not set permanently, but your ordinary settings will prevail
when you are working outside the script, no matter how it was terminated.
The variable will revert to whatever value is assigned by your .bashrc, or
whatever. Otherwise any script that hangs and is terminated by Ctrl-C
would still affect things.
Doug.
--
ICQ Number 178748389. Registered Linux User No. 277548.
People want economy and they will pay any price to get it.
- Lee Iacocca. |
|
| Back to top |
|
 |
AT *nix forums Guru Wannabe
Joined: 22 Feb 2005
Posts: 119
|
Posted: Fri Feb 18, 2005 9:13 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
On Thu, 17 Feb 2005 22:35:12 +0100, Ulf Meinhardt wrote:
| Quote: | 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.
|
I may be wrong, but aren't variables local to the running shell, in this
case the shell under which the script is running (hint:#!/bin/bash)? This
will not influence the variables set in your login shell.
HTH
Andreas |
|
| Back to top |
|
 |
Bill Marcum *nix forums Guru
Joined: 28 Mar 2005
Posts: 1264
|
Posted: Fri Feb 18, 2005 7:00 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
On Thu, 17 Feb 2005 22:35:12 +0100, Ulf Meinhardt
<ulf2m@email.com> wrote:
| Quote: | 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?
Not necessary. The script runs in a subshell; it receives a copy of the |
parent environment.
> |
|
| Back to top |
|
 |
Alexander Skwar *nix forums addict
Joined: 03 May 2005
Posts: 59
|
Posted: Fri Feb 18, 2005 4:28 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
· Ulf Meinhardt <ulf2m@email.com>:
| Quote: | 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?
|
You don't need to. The script is run in a subshell. Subshells cannot
influence env. vars of parent shells.
Given the following script:
#!/bin/sh
export FOO=bar
echo FOO in Script: $FOO
exit
When you do:
FOO=blubb
../mini-script.sh
echo $FOO
| Quote: | The env variable has no chance to get back its original value.
|
Yes, it does.
Alexander Skwar
--
(null cookie; hope that's ok)
·_·¯·_·¯·_ ♘♞♟♙♖♜ аäöüßÄÖÜæœłø¼½¾¤¹²³¢€£¥¶§¬÷×±©®™¡¿ ♪♬♯♪♬♯♠¯·_·¯·_·¯· |
|
| Back to top |
|
 |
Alan D Johnson *nix forums addict
Joined: 24 Feb 2005
Posts: 71
|
Posted: Fri Feb 18, 2005 3:29 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
Ulf Meinhardt wrote:
| Quote: | 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
exit routine |
export BLA= |
|
| Back to top |
|
 |
Barry Margolin *nix forums Guru
Joined: 24 Feb 2005
Posts: 323
|
Posted: Fri Feb 18, 2005 2:52 am Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
In article <cv32mg$js5$05$1@news.t-online.com>,
ulf2m@email.com (Ulf Meinhardt) wrote:
| Quote: | 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 *** |
|
| Back to top |
|
 |
Chris McDonald *nix forums Guru Wannabe
Joined: 01 Mar 2005
Posts: 107
|
Posted: Thu Feb 17, 2005 10:35 pm Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
ulf2m@email.com (Ulf Meinhardt) writes:
| Quote: | 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.
|
If the shell crashes, then your shellscript will terminate (hopefully
with failure), so there is no meaning to "switch back to the original value...".
______________________________________________________________________________
Dr Chris McDonald E: chris@csse.uwa.edu.au
Computer Science & Software Engineering W: http://www.csse.uwa.edu.au/~chris
The University of Western Australia, M002 T: +618 6488 2533
Crawley, Western Australia, 6009 F: +618 6488 1089 |
|
| Back to top |
|
 |
Chris F.A. Johnson *nix forums Guru
Joined: 20 Feb 2005
Posts: 2268
|
Posted: Thu Feb 17, 2005 9:41 pm Post subject:
Re: Is there an "undo" for export at the end of a shell script?
|
|
|
On Thu, 17 Feb 2005 at 21:35 GMT, Ulf Meinhardt wrote:
| Quote: | 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?
|
There's no need; once the shell dies, the change dies with it.
| Quote: | 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.
|
The change is only effective in that shell or processes spawned by
it. Whether the script finishes or noy, it will not affect the
environment of the shell that called it (unless it was sourced).
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License |
|
| Back to top |
|
 |
Ulf Meinhardt *nix forums beginner
Joined: 17 Feb 2005
Posts: 1
|
Posted: Thu Feb 17, 2005 8:35 pm Post subject:
Is there an "undo" for export at the end of a shell script?
|
|
|
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 |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|