setenv <name> <value>
if i execute the same it not working.Is there any specific way in tcl
where in we can set the variable.
Thanks
chendu
e.g.
set ::env(PATH) /usr/local/bin
Michael
For those that like setenv...
proc setenv {name value} {
set ::env($name) $value
}
Donal.
Understand, this does NOT set the environment of the process which
*invokes* a Tcl application; the latter is often what questioners
think they want <URL: http://wiki.tcl.tk/4282 >.
Mind you, nothing does except editing a file/registry entry[*] and
restarting the shell or reloading its configuration. This is by
deliberate design, and applies to thing other than Tcl.
Donal.
[* Depending on platform. ]
source `my_example.tcl`
or
eval ... $EXECUTABLE ...
or similar trick mentioned in that same Wiki page often
comes as a revelation to those asking questions in this
area.
Sufficient knowledge of the internal data structures of the platform
lets you do theoretically impossible things. In my early career as a
software developer, I wrote a large collection of DOS programs that
communicated with each other by manipulating the "root" environment.
(There was much about that project I should have done differently, or
not done at all. Basing the entire architecture of the user interface
and network communication around the capabilities of a Hercules Network
Card Plus made for a fast and reasonably pretty system, but when the
supply of monochrome monitors started to dry up...)
You can't do that on "modern" systems, i.e. anything with virtual memory
or real memory protection! You either can't see the root environment at
all, or can't recognize it, or can't write it (the most common case.)
Donal.
You can, if you have enough privileges -- or at least the right ones.
After all that is how ACPs work.
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
i am able to set env variable and able to get it with in the same tcl
script .
But if i want to access in the value in other tcl script with out
editing the shell rc file.
Is there any way
Process:
1.I would open a shell.
2.run a tcl script to set a env variable
3.run a second tcl script to accss the env variable set by 1st
script.
i tried using as in wiki
source export myvar=<value>
but i am not able to get it. basically i am not able to get what
export does.
Thanks
chendu
No. That's not how environment variables work. This is not something
under Tcl's control. If a process sets environment variables, the only
other processes that can see those changes are processes spawned by the
one that changes the environment (assuming those variables are properly
exported)
> i tried using as in wiki
> source export myvar=<value>
> but i am not able to get it. basically i am not able to get what
> export does.
Export says "make this available to any child this process spawns", and
it's not a tcl command, it's a command in some shells.
You might be interested to read more about environment variables on the
wikipedia: http://en.wikipedia.org/wiki/Environment_variable
No.
For steps 2 and 3 you could replace the string "tcl script" and "1st script"
with "a program I wrote" and be correct -- at least for anyone needing to
ask the question.
As others have said, the child process cannot write variables in the
parent process.
> Is there any way
> Process:
> 1.I would open a shell.
> 2.run a tcl script to set a env variable
> 3.run a second tcl script to accss the env variable set by 1st
> script.
What you do is have the child process suggest changes to the parent, but
the parent has to decide what to do with it:
1. open a shell
2. call the tcl "config" script: it prints to stdout like this:
export var1=val1
export var2=val2
Use whatever syntax your shell requires.
3. your shell captures and evaluates the output of #2.
4. ?
5. Profit! (with apologies to /.)
Like this:
$ cat var.tcl
#! /usr/local/bin/tclsh
puts "export FOO=bar"
puts "export BAZ=qux"
$ echo "FOO='$FOO', BAZ='$BAZ'"
FOO='', BAZ=''
$ eval `./var.tcl`
$ echo "FOO='$FOO', BAZ='$BAZ'"
FOO='bar', BAZ='qux'
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
What do you mean by "it [is] not working"?
Can you give us a specific example of what you are trying to
accomplish?
On what platform are you running? For instance, on Unix, something
like this would work.
$ MYVARIABLE=$(firsttclscript)
$ export MYVARIABLE
$ SecondTclScriptThatUsesMYVARIABLE
The question nobody has asked is why you feel the need to use
environment variables for this. Environment variables are a very limited
form of interprocess communication, and their restrictions are such that
it's very difficult to do the sort of thing you're trying to do.
Both Linux and Windows have a plethora of IPC mechanisms, most of which
are trivial to access from Tcl. Environment variables are just the wrong
tool for this.
I suggest writing values to a temporary file, a named pipe, a shared
memory segment, or a socket.
--
Darren New / San Diego, CA, USA (PST)
Remember the good old days, when we
used to complain about cryptography
being export-restricted?