Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get env variable

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I would like to check that some variables in the environment are set properly or simply exists.

If I do:
"set test $env(VAR)" and VAR does not exist I get an error message "VAR in not ENV array" How can I go pass this if VAR does not exist and I still want to be able to run the program and exit with my own error message.

I could go through all the elements of ENV array and check each one but I am sure there is a better way to do this.

Thanks!
 
You can do:
Code:
  catch { set test $env(VAR); puts $test }
  if {[info exists env(VAR)]} { puts $env(VAR) }
The first line catches the fired error if env(VAR) does not exist.
The second line tests explicitely for existance of env(VAR).

ulis
 
I think you'll find "catch" is what you want. For example:

%set abc
can't read "abcv": no such variable
%set rtc [catch {set abc}]
1
%set abc 5
5
%set rtc [catch {set abc}]
0 Bob Rashkin
rrashkin@csc.com
 
thanks Ulis this worked fine!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top