How can I avoid the error: there is no such environment variable?

In my code I use environment variables, but if it (env.var) does not exist, I get the error NAME_ENV_VAR: there is no such variable, and my script stops running. For example, in the line

myeval $env($File) 

I get an error message:

  can't read "env(NIKE_TECH_DIR)": no such variable while executing "myeval $env($File)" (procedure "chooseRelevantFiles" line 39) invoked from within "chooseRelevantFiles $::GlobalVars::reqStage" (file "/vobs/tavor/src/Scripts/ReproduceBug.tcl" line 575) 

How can I avoid this error and continue to execute the script?

+8
tcl
source share
3 answers

You can test with info exists and use the default value if the environment variable is not set, for example.

 if {[info exists env($File)]} { set filename $env($File) } else { set filename /some/default/path } myeval $filename 
+15
source share

catch error, then you can do something with it (for example, register it later or use the return value) and continue using the script

eg.

 if {[catch {myeval $env($File)} result]} { lappend log $result } #other stuff 
+6
source share

To check an array element, such as the env global array, do not use [info exists $env(VAR)] . Instead, you should use:

 if { [ array names env VAR ] != "" } { puts "\nVAR exists and its value is $env(VAR)\n" } 
+3
source share

All Articles