#!/bin/bash # author: P.Zumbruch, GSI # change: Nov 6, 2008 # Last change: Aug 9, 2013 indirectExpansion() { if [ "$1" = "-h" ] then cat << EOF # function indirectExpansion # # echos parameter expansion of given parameter # # usage: # indirectExpansion parameter # EOF return fi if [ $# -ge 1 ] then echo -n "$(eval echo \${$1})" else return fi } checkAndCleanPath() { if [ "$1" = "-h" ] then cat << EOF # function checkAndCleanPath # # checks for (a given) pathname (default:PATH) # whether all directories are valid # if not, they are removed from the path # # usage: # checkAndCleanPath [pathname] # # pathname: name of the variable, if not set PATH is used EOF return fi if [ $# -eq 0 ] then name=PATH else name=$1 fi myCheckPath=$(indirectExpansion $name) newPath= for dir in $(echo ${myCheckPath//:/ }) do if [ -d $dir ] then if [ -z "$newPath" ] then newPath=${dir} else newPath=${newPath}:${dir} fi fi done eval ${name}=${newPath} unset IFSold myCheckPath newPath name } ifVerboseEcho() { if [ "$1" = "-h" ] then cat << EOF # function ifVerboseEcho # # echos value of the given environment in a defined, # but only if the variable "verbose" is different from 0 # # usage: # ifVerboseEcho # EOF return fi if [ $verbose ] then echo -e $1 "\r\t\t\t" set to $(indirectExpansion ${1}) fi } addToPath() { if [ "$1" = "-h" ] then cat << EOF # function addToPath # # adds to (a given) path variable another element # *in front of* or *behind* the other elements # if \$verbose is set, than an note is written to STDOUT, # if a new element has been added. # if \$uniqpathsScript is available the path is checked for double entries # # usage: # addToPath element [pathname [reverse]] # # element: any valid path, if invalid it's ignored # pathname: name of the variable, if not set PATH is used # reverse: element is added behind of the existing elements, # if not set in front of. # # addToPathEnd(): is an acronym for the reverse mode of addToPath() EOF return fi addToPathUnset() { unset reverse pathname prepath postpath adding position uniqpaths scriptName preClean } scriptName=fcn_addtopath.bash uniqpathsScript=${uniqpathsScript:=~/bin/uniqpaths} adding=$1 #just add valid paths checkAndCleanPath adding if [ -z $adding ] then addToPathUnset return fi if [ $# -eq 1 ]; then pathname=PATH; else pathname=$2; fi reverse="" if [ $# -eq 3 ] then if [ $3 != "reverse" ] then echo Warning \(addtoPath\): argument 3: \"$3\" may only be \"reverse\" or empty nothing done addToPathUnset return fi reverse=TRUE fi #prepath: path before addings prepath=$(indirectExpansion ${pathname}) if [ ${reverse} ] then eval $pathname=$prepath:$adding position="behind" else eval $pathname=$adding:$prepath position="in front of" fi #remove doubles in path if [ -f ${uniqpathsScript} ] then eval ${pathname}=$(echo $(indirectExpansion ${pathname}) | perl -w ${uniqpathsScript}) else echo Warning: ${scriptName}: "\`${uniqpathsScript}\'" not available, there may be doubles in \$${pathname}. '(if still available, export uniqpathsScript=)' fi postpath=$(indirectExpansion ${pathname}) [ $verbose ] && if [ ${prepath} != ${postpath} ]; then echo -e "added $adding $position ${pathname}"; fi addToPathUnset } addToPathEnd() { if [ "$1" = "-h" ] then cat << EOF # function addToPathEnd # # ... is an acronym for the reverse mode of addToPath() # # usage: # addToPathEnd element [pathname] # # element: any valid path # pathname: name of the variable, if not set PATH is used EOF return fi adding=$1 if [ $# -eq 1 ] then pathname=PATH else pathname=$2 fi addToPath $adding $pathname "reverse" unset adding pathname }