Read Bash environment variable in TCL
How to read a shell environment variable in your Tcl script. So anyone开发者_JAVA技巧 please help me. I am very new in TCL.
Use $::env
to access any environment variables e.g. to access the TMP environment variable do this:
set tmpdir $::env(TMP)
More info here http://wiki.tcl.tk/1624
$ export var=42
$ tclsh
% puts $env(var)
42
Environment variables are accessible via the built-in global variable env
(fully qualified it is ::env
). You use this like any other Tcl array.
If you want to print a list of all environment variables you can use something like this:
proc dump_env_vars {} {
foreach name [array names ::env] {
puts "$name == $::env($name)"
}
}
Of course, to access just a single variable you use it like any other array, for example:
puts "HOME = '$::env(HOME)'"
For more information see the env page on the Tcler's wiki and the env section of the tclvars man page
To read a shell environment variable in Tcl script, try doing something like this:
global env
set olddisplay $env(DISPLAY)
set env(DISPLAY) unix:0
This might be expressed as well in this fashion:
set olddisplay $::env(DISPLAY)
set ::env(DISPLAY) unix:0
and forget about global
.
You can check to see if the variable exists by doing something like:
if {[info exists env(VARNAME)]} {
# okay, it's there, use it
set value $env(VARNAME)
} else {
# the environment var isn't set, use a default
set value "the default value"
}
This is source.
精彩评论