mirror of
https://github.com/Pmodules/Pmodules.git
synced 2026-06-27 18:13:08 +02:00
cleanup initialisation
- Pmodules/bash: contains now only the 'module()' function, some sanity checks and initialization of bash completion plus unsetting some unused variables. - Pmodules/libstd.bash: bug fixed in std::append_path if path includes spaces - Pmodules/modulecmd.bash.in: (re-)initialize if 'PMODULES_ENV' is empty or unset
This commit is contained in:
+35
-8
@@ -67,16 +67,16 @@ std::get_abspath() {
|
||||
}
|
||||
|
||||
std::append_path () {
|
||||
local -r P=$1
|
||||
local -r d=$2
|
||||
local -r P="$1"
|
||||
local -r d="$2"
|
||||
|
||||
if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then
|
||||
if [[ -z ${!P} ]]; then
|
||||
eval $P=${d}
|
||||
else
|
||||
eval $P=${!P}:${d}
|
||||
fi
|
||||
fi
|
||||
if [[ -z ${!P} ]]; then
|
||||
eval $P=\"${d}\"
|
||||
else
|
||||
eval $P=\"${!P}:${d}\"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
std::prepend_path () {
|
||||
@@ -105,6 +105,33 @@ std::remove_path() {
|
||||
eval ${P}="${new_path:1}"
|
||||
}
|
||||
|
||||
#
|
||||
# Replace or remove a directory in a path variable.
|
||||
#
|
||||
# To remove a dir:
|
||||
# std::replace_path PATH <pattern>
|
||||
#
|
||||
# To replace a dir:
|
||||
# std::replace_path PATH <pattern> /replacement/path
|
||||
#
|
||||
# Args:
|
||||
# $1 name of the shell variable to set (e.g. PATH)
|
||||
# $2 a grep pattern identifying the element to be removed/replaced
|
||||
# $3 the replacement string (use "" for removal)
|
||||
#
|
||||
# Based on solution published here:
|
||||
# https://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts
|
||||
#
|
||||
std::replace_path () {
|
||||
local -r path="$1"
|
||||
local -r removepat="$2"
|
||||
local -r replacestr="$3"
|
||||
|
||||
local -r removestr=$(echo "${!path}" | tr ":" "\n" | grep -m 1 "^$removepat\$")
|
||||
export $path="$(echo "${!path}" | tr ":" "\n" | sed "s:^${removestr}\$:${replacestr}:" |
|
||||
sed '/^\s*$/d' | tr "\n" ":" | sed -e 's|^:||' -e 's|:$||')"
|
||||
}
|
||||
|
||||
#
|
||||
# split file name
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user