Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Environment variables

854 views
Skip to first unread message

Marius Urkis

unread,
Jun 21, 1995, 3:00:00 AM6/21/95
to

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

Jay Goldberg

unread,
Jun 22, 1995, 3:00:00 AM6/22/95
to
There is a global variable 'env' that contains the environment variables

proc x {} {
global env

echo "$env(VARIABLE)"
}

Jay
ja...@accessware.com


Jeffrey Hobbs

unread,
Jun 22, 1995, 3:00:00 AM6/22/95
to
In article <Pine.SUN.3.91.950621172359.7916A-100000@bosas>,

Marius Urkis <Marius@bosas> wrote:
>I have a question on Tcl. How can I check for any environment variable
>presence?

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/

Roger Critchlow

unread,
Jun 22, 1995, 3:00:00 AM6/22/95
to
In article <3sc251$r...@uuneo.neosoft.com> Jay Goldberg <ja...@accessware.com> writes:

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 --

Joe Moss

unread,
Jun 22, 1995, 3:00:00 AM6/22/95
to
marius urkis <marius@bosas> writes:

>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

Hiroshi Minagawa

unread,
Jun 23, 1995, 3:00:00 AM6/23/95
to
Marius Urkis (Marius@bosas) wrote:

: 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

Michael Schumacher

unread,
Jul 5, 1995, 3:00:00 AM7/5/95
to
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

PS: Please fix your email address.

Bill Mullen

unread,
Jul 8, 1995, 3:00:00 AM7/8/95
to
In article <3tddjr$d...@hades.rz.uni-sb.de> hig...@rz.uni-sb.de (Michael Schumacher) writes:

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

Rick Kramer

unread,
Jul 9, 1995, 3:00:00 AM7/9/95
to

> In article <3tddjr$d...@hades.rz.uni-sb.de> hig...@rz.uni-sb.de (Michael Schumacher) writes:

> 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

Stephen Peters

unread,
Jul 10, 1995, 3:00:00 AM7/10/95
to
In article <BILLM.95J...@barney.synopsys.com> bi...@barney.synopsys.com (Bill Mullen) writes:
>set value "default_value"
>foreach i [array names env] {
> if {($i == "VARIABLE")} {
> set value $env($i)
> }
>}
>
>Is there a cleaner way?

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

Mark J. Crosland

unread,
Jul 11, 1995, 3:00:00 AM7/11/95
to
how about

# 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

0 new messages