在我的代码中我使用环境变量,但如果它(env.var)不存在,我得到错误消息NAME_ENV_VAR:没有这样的变量,我的脚本停止执行.例如,在行中
myeval $env($File)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
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)
Run Code Online (Sandbox Code Playgroud)
如何避免此错误并继续执行我的脚本?
Col*_*eod 18
info exists如果未设置环境变量,您可以测试并使用默认值,例如.
if {[info exists env($File)]} {
set filename $env($File)
} else {
set filename /some/default/path
}
myeval $filename
Run Code Online (Sandbox Code Playgroud)
捕获错误,然后你可以用它做一些事情(例如,以后记录它,或使用后退值)并继续你的脚本
例如
if {[catch {myeval $env($File)} result]} {
lappend log $result
}
#other stuff
Run Code Online (Sandbox Code Playgroud)