TCL: how to check if environment variable already set
Asked Answered
M

3

10

I am trying to construct a module file and I would like to include an if statement to check whether certain environment variables (e.g. PATH, LD_LIBRAY_PATH, PYTHON_PATH, ...) have already been set.

I have tried several different syntax options (after searching on here, official documentation and other forums) but when I then try:

module use modulefiles
module load mymodule

I keep getting ERROR:102: Tcl command execution failed error. Here are a few of the options that I have tried:

i)

set myTools /path/to/my/Tools
if { info exists $LD_LIBRARY_PATH } {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTool/lib64:$LD_LIBRARY_PATH
} else {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTools/lib64
}

ii)

set myTools /path/to/my/Tools
if { [info exists $LD_LIBRARY_PATH] } {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTool/lib64:$LD_LIBRARY_PATH
} else {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTools/lib64
}

iii)

set myTools /path/to/my/Tools
if { $?LD_LIBRARY_PATH } {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTool/lib64:$LD_LIBRARY_PATH
} else {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTools/lib64
}

What is the correct syntax to check for the existence of environment variables that may have already been set, e.g. in a .cshrc or .login file?

Apologies if this is a basic question. I've done stuff like this before in the shell (mainly csh), but I am new to TCL and building module files, so any help and advice wold be greatly appreciated!

Thanks!

Marolda answered 7/5, 2016 at 6:42 Comment(0)
B
11

The http://wiki.tcl.tk/1624 about env contains probably the answer to your question.

Following the hints given in the other answers and the wiki link given above I constructed following code which works for me.

if { [info exists ::env(IDL_PATH)] } {
   if {![string match *<IDL_DEFAULT>* $::env(IDL_PATH)]} {
   puts stderr "Warning: ill defined IDL_PATH environment variable
   }
} else {
setenv  IDL_PATH        "<IDL_DEFAULT>"
}
Bug answered 8/9, 2016 at 14:13 Comment(0)
N
3

It really helps if you have the actual error message and not the summary “something went wrong”.

Generally speaking, environment variables are mapped into elements of the global env array in Tcl; they don't just become individual global variables (as that would be open to too much abuse). Also, when talking about a variable you don't read from it and so don't prefix it with $ (which in Tcl is strictly a read operation). That said, using the setenv is important because that is about manipulating the environment in the caller, not in the Tcl code itself.

So we use a variation on your second attempt.

set myTools /path/to/my/Tools
if { [info exists env(LD_LIBRARY_PATH)] } {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTool/lib64:$env(LD_LIBRARY_PATH)
} else {
    setenv LD_LIBRARY_PATH $myTools/lib:$myTools/lib64
}

The problem with the first attempt is that you need to use square-bracket syntax to use the output of a Tcl command. The problem with the third one is that $? doesn't mean anything to Tcl at all (I guess we could change that, but it would be a long road to walk and it doesn't help you now).

Normally answered 7/5, 2016 at 6:58 Comment(0)
E
3

the env global array could be also used to set the value of the environment variable, and it is better to use ::env instead of env to access it from the level 0 (or declare it as global env)

set myTools /path/to/my/Tools
if { [info exists ::env(LD_LIBRARY_PATH)] } {
    set ::env(LD_LIBRARY_PATH) $myTools/lib:$myTool/lib64:$env(LD_LIBRARY_PATH)
} else {
    set ::env(LD_LIBRARY_PATH) $myTools/lib:$myTools/lib64
}
Epochmaking answered 8/5, 2016 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.