#!/bin/bash # echo loading module library module_confirm() { local FILE local DIR local NAME DIR=$1 NAME=$2 # echo finding $NAME in $DIR FILE="$(command find ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh)" # echo files found $FILE COUNT=0 if [ "$FILE" ]; then COUNT=$(echo $FILE | xargs -n 1 | wc -l) fi # echo Number: $COUNT if [ $COUNT -gt 1 ]; then echo two or more modules of same name found, aborting echo $FILE | xargs -n 1 return 1 fi [ $COUNT == 1 ] && echo $FILE && return 0 return 1 } # if succesfull returns the path module_find() { [ ! $1 ] && echo "no module specified" && return 1 local MDIRS local MDIR local DIRS local MODULE=$1 # MODULE_DIRS is array set in the environment and is an array of additional directories to # search for modules. This makes is easy to include a custom module libraries outside # the shell system. These take precedence over any modules found in shell directories below if [[ $MODULE_DIRS ]]; then for DIR in "${MODULE_DIRS[@]}" do if [[ -d $DIR ]]; then RES=$(module_confirm "$DIR" "$MODULE") [ $? -eq 0 ] && echo $RES && return 0 fi done fi # BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched # in revsere order, user or network repos over host over base DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ) [ -d $HOME/$BASH_SHELL_USER ] && DIRS=("${DIRS[@]}" "$HOME/$BASH_SHELL_USER") cnt=${#DIRS[@]} for ((i=1;i<=cnt;i++)); do # find modules in reverse order so more specific ones override DIR="${DIRS[cnt-i]}/modules" if [[ -d $DIR ]]; then RES=$(module_confirm "$DIR" "$MODULE") [ $? -eq 0 ] && echo $RES && return 0 fi done # no module found anywhere return 1 } module_load() { [ ! $1 ] && echo "no module specified" && return 1 # (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed" local FILE FILE=$(module_find $1) [ $? -ne 0 ] && echo no module $1 found && return 1 # source $FILE "$0" # [[ $BASHPID -eq $$ ]] || echo $FILE source $FILE return 0 } module_print() { [ ! $1 ] && echo "no module specified" && return 1 # (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed" local FILE FILE=$(module_find $1) [ $? -ne 0 ] && echo no module $1 found && return 1 cat $FILE return 0 } module_loaded() { [ ! $1 ] && echo "no module specified" && return 1 local func local ret local file ret=0 # function_list | grep module file=$(module_find $1) [ ! $file ] && echo no module $1 found && return 1 # echo "$(function_list $file)" for func in $(function_list $file) do [ ! $(function_list | grep $func) ] && ret=1 [[ $2 = -vv ]] && ([[ $ret = 0 ]] && echo "$func loaded" || echo "$func not loaded") done [[ $2 = -v ]] && echo "module: $1" && ([[ $ret = 0 ]] && echo "all functions " || echo "some or all functions not loaded") [[ $ret = 0 ]] && echo "loaded" return $ret } # exported for login environment # declare -f -x module_load # declare -f -x module_find # declare -f -x module_confirm # uncomment for testing # function _test_modules { # RED='\033[0;31m' # NC='\033[0m' # No Color # echo module to test: $1 # echo -------------------- # echo -e "testing: ${RED}module_find${NC}" # FILE=$(module_find $1) # [ $? -ne 0 ] && echo no module $1 found && return 1 # echo module $FILE found # # . $FILE # echo --------------- # echo -e "testing: ${RED}module_load${NC}" # module_load $1 # [ $? -ne 0 ] && echo no module $1 found && return 1 # echo loaded module $1 # echo ---------------------- # echo -e "testing: ${RED}module_loaded${NC}" # module_loaded $1 # }