From 79e5b77df2ef8fdbfc737323c9c66b8bf995ebc1 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Tue, 23 Aug 2016 13:55:51 -0400 Subject: [PATCH] * bash-module-env.sh: Update was required due to incomplete pre-existing MODULEPATH in some of Turing's compute nodes. --- turing/support/bash-module-env.sh | 52 +++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/turing/support/bash-module-env.sh b/turing/support/bash-module-env.sh index 2a6e8ac..b302de5 100755 --- a/turing/support/bash-module-env.sh +++ b/turing/support/bash-module-env.sh @@ -1,5 +1,22 @@ # -*- bash -*- # +# BASH support scriplet for environment modules. +# +# Simply "source" this script with "init" argument, +# then you can use "module" command from the batch script. +# +# Example (from within your batch script): +# +# source ~/local/bash-module-env.sh init +# module load gcc +# +# Optional: if you want to restore the modules listed in $LOADEDMODULES +# (which will need to be passed on to the script via "-v LOADEDMODULES" argument +# when qsub-ing, then use "restore" argment instead: +# +# Example (from within your batch script): +# +# source ~/local/bash-module-env.sh restore # # Some cluster defaults (may change over time) @@ -17,7 +34,7 @@ MODULEPATH_DEFAULT=/cm/local/modulefiles:/cm/shared/modulefiles:/cm/shared/compi RestoreModuleEnv () { local LOADEDMODULES_SAVE : ${MODULESHOME:=$MODULESHOME_DEFAULT} - : ${MODULEPATH:=$MODULEPATH_DEFAULT} + _AppendPaths MODULEPATH "$MODULEPATH_DEFAULT" export MODULESHOME export MODULEPATH @@ -39,15 +56,44 @@ RestoreModuleEnv () { InitModuleEnv () { # Only do the initialization part of the module so "module" command # is available for the script - local LOADEDMODULES_SAVE : ${MODULESHOME:=$MODULESHOME_DEFAULT} - : ${MODULEPATH:=$MODULEPATH_DEFAULT} + _AppendPaths MODULEPATH "$MODULEPATH_DEFAULT" + #: ${MODULEPATH:=$MODULEPATH_DEFAULT} export MODULESHOME export MODULEPATH source "$MODULESHOME/init/bash" } +_AppendPath () { + # Usage: AppendPath VARNAME A_PATH + # Appends a path element if it doesnt exist in current path + local VAR VAL P + VAR=$1 + P=$2 + VAL=$(eval "echo \"\$$VAR\"") # gets the current value of variable ${!VAR} + case ":${VAL}:" in + *:"${P}":*) + ;; + *) + VAL="${VAL}:${P}" + eval "$VAR=\$VAL" + ;; + esac +} + +_AppendPaths () { + # Usage: AppendPath VARNAME PATHLIST + # Appends one or more elements from PATHLIST for the elements that don't + # exist in current path + local VAR PATH1 PATHLIST + VAR=$1 + PATHLIST=$2 + for PATH1 in ${PATHLIST//:/" "}; do + _AppendPath "$VAR" "$PATH1" + done +} + # For convenience: if [ "$1" = restore ]; then RestoreModuleEnv