Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL: how to check if environment variable already set

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!

like image 839
aim Avatar asked May 07 '16 06:05

aim


People also ask

How do I check if an environment variable is set?

On Windows In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.

How do we know if a variable exists or not in Tcl?

To check whether a variable exists precisely in a certain namespace, fully-qualify the variable name. info exists returns 0 for variables that exist but are undefined. This can happen, for example, with trace, because if a trace is set on a nonexisting variable, trace will create that variable, but leave it undefined.

How do you view all current environment variables?

The printenv command-line utility displays the values of environment variables in the current shell. We can specify one or more variable names on the command line to print only those specific variables. Or, if we run the command without arguments, it will display all environment variables of the current shell.

How do you call an environment variable in Tcl?

Environment variables are available to Tcl scripts in a global associative array env . The index into env is the name of the environment variable. The command puts "$env(PATH)" would print the contents of the PATH environment variable.


1 Answers

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>"
}
like image 63
kcbehler Avatar answered Oct 09 '22 20:10

kcbehler