I have a question on Tcl. How can I check for any environment variable
presence? For example in csh script such sentence looks like this:
if ($?VARIABLE) then
....
endif
How it would be in Tcl?
Thanks.
Marius Urkis
Kaunas University of Technology
e-mail: Mar...@soften.ktu.lt
proc x {} {
global env
echo "$env(VARIABLE)"
}
Environment variables are kept in the global array 'env'. To check for
the existence of any particular variable (ie - HOME):
if [info exists env(HOME)] {
puts "Found home: $env(HOME)"
} else {
puts "No home environment variable"
}
To print out all the environment variables:
% foreach a [array names env] { puts "env($a) = $env($a)" }
Remember to use global when accessing env from inside a proc.
--
Jeffrey Hobbs Office: 503/346-3998
Univ of Oregon CIS GTF email: jho...@cs.uoregon.edu
URL: http://www.cs.uoregon.edu/~jhobbs/
There is a global variable 'env' that contains the environment variables
proc x {} {
global env
echo "$env(VARIABLE)"
}
But you actually want to test for existence before accessing a possibly
nonexistent value:
proc getenv {variable {default {}}} {
global env;
if {[info exists env($variable)]} {
return $env($variable);
} else {
return $default;
}
}
-- rec --
>I have a question on Tcl. How can I check for any environment variable
>presence? For example in csh script such sentence looks like this:
>if ($?VARIABLE) then
> ....
>endif
>How it would be in Tcl?
See <http://route.psg.com/tcl.html#EnvironmentVariables>
--
Joe V. Moss | Hm: j...@italia.rain.com Wk: j...@morton.rain.com
Morton & Associates |--------------------------------------------------
7478 S.W. Coho Ct. | label .l -text "Insert cute quote here"
Tualatin, OR 97062 | button .b -text "OK" -command exit; pack .l .b
: Hello,
: I have a question on Tcl. How can I check for any environment variable
: presence? For example in csh script such sentence looks like this:
: if ($?VARIABLE) then
: ....
: endif
: How it would be in Tcl?
: Thanks.
: Marius Urkis
: Kaunas University of Technology
: e-mail: Mar...@soften.ktu.lt
You can write it as:
if {[info exist env(VARIABLE)]} {
...
}
Regards,
========================================= ________
Hiroshi Minagawa i...@ti.com (email) / __|__\ \
| | |
Texas Instruments +81-3-3769-3208 | _/ \__ |
Japan Ltd. +81-3-3457-6776 (FAX) \________/
========================================= - Inu and Only Inu
global env
if { $env(VARIABLE) } { ;# VARIABLE is the name of the env. var.
}
mike
PS: Please fix your email address.
From: hig...@rz.uni-sb.de (Michael Schumacher)
Newsgroups: comp.lang.tcl
Date: 5 Jul 1995 07:05:31 GMT
Organization: HighTec EDV-Systeme GmbH
Reply-To: mi...@hightec.saarlink.de
Marius Urkis (Marius@bosas) wrote:
^^^^^ !
: if ($?VARIABLE) then
: ....
: endif
: How it would be in Tcl?
global env
if { $env(VARIABLE) } { ;# VARIABLE is the name of the env. var.
}
mike
The problem with this solution is that the source command will halt
if VARIABLE is not defined, with an error like this:
can't read "env(VARIABLE)": no such element in array
I would like a solution that checks existence of VARIABLE within a
script, and sets it to a default value if it is not already defined.
The only way I've found to do this is with the array names function:
set value "default_value"
foreach i [array names env] {
if {($i == "VARIABLE")} {
set value $env($i)
}
}
Is there a cleaner way?
Thanks for any ideas,
Bill Mullen
--
Bill Mullen bi...@synopsys.com
Synopsys, Inc. (415) 694-4316
700 E. Middlefield Rd. (415) 965-8637 (fax)
Mountain View CA 94043
> The problem with this solution is that the source command will halt
> if VARIABLE is not defined, with an error like this:
>
> can't read "env(VARIABLE)": no such element in array
>
> Is there a cleaner way?
I think so..
if { [info exists env(VARIABLE)] } {
# it exists
} else {
# it doesn't
}
or you could use a much cheesier hack
if { [catch { set env(VARIABLE) }] == 1 {
# error, it doesn't exist
} else {
# no error, it exists
}
Hope that helps..
-rick
I usually use something like
set value "default_value"
catch {set value $env(VARIABLE)}
which does the job nicely -- if VARIABLE doesn't exist, the catch command
will fail.
Stephen Peters
# returns -1 if $id is not in the current environment
proc env_exists { id } {
global env
set envset [array names env]
set is_it_there [lsearch $envset $id]
return $is_it_there
}
mark crosland
m...@oz.net