开发者

How can I avoid an error: No such environment variable?

开发者 https://www.devze.com 2023-04-12 03:27 出处:网络
In my code I am using environment variables, but if it (env.var) doesn\'t exist, I get the error message NAME_ENV_VAR: no such variable, and my script stops executing.

In my code I am using environment variables, but if it (env.var) doesn't exist, I get the error message NAME_ENV_VAR: no such variable, and my script stops executing. For example, in the line

开发者_如何学C
 myeval $env($File)

I receive an error:

 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 go on to execute my script?


You could test with info exists and use a default if the environment variable is not set, eg.

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


catch the error then you can do something with it (e.g. log it for later, or use a fall back value) and proceed with your script

e.g.

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


To check for of an array element like global env array, don't use [info exists $env(VAR)]. Instead, you should use:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号