Commit df9adc82 authored by gsell's avatar gsell
Browse files

scripts/Bootstrap/Pmodules/libpmodules.bash:

- set 'bindir' if not set
- '\n' removed from info() and error()
- die() always prints to stderr
- get_abspath(), append_path(), prepend_path(), remove_path() added
parent 2d0b29ec
#!/bin/bash
if [[ -z ${bindir} ]]; then
local bindir=$(dirname "${BASH_SOURCE}")
bindir=$(cd "${bindir}"/.. && pwd)"/bin"
fi
log() {
local -ri fd=$1
local -r fmt="$2\n"
......@@ -8,11 +13,11 @@ log() {
}
info() {
log 2 "$1\n" "${@:2}"
log 2 "$1" "${@:2}"
}
error() {
log 2 "$1\n" "${@:2}"
log 2 "$1" "${@:2}"
}
debug() {
......@@ -23,16 +28,10 @@ debug() {
die() {
local -ri ec=$1
shift
local cout
if (( ec == 0)); then
cout='1'
else
cout='2'
fi
if [[ -n $@ ]]; then
local -r fmt=$1
shift
log $cout "$fmt" "$@"
log 2 "$fmt" "$@"
fi
exit $ec
}
......@@ -54,6 +53,20 @@ get_YN_answer() {
esac
}
#
# return normalized abolute pathname
# $1: filename
get_abspath() {
local -r fname=$1
[[ -r "${fname}" ]] || return 1
if [[ -d ${fname} ]]; then
echo $(cd "${fname}" && pwd)
else
local -r dname=$(dirname "${fname}")
echo $(cd "${dname}" && pwd)/$(basename "${fname}")
fi
}
get_options() {
"${bindir}/getopt" "$@"
}
......@@ -85,6 +98,45 @@ check_pmodules_env() {
check_pmodules_directories "${PSI_PREFIX}"
}
append_path () {
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
}
prepend_path () {
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=${d}:${!P}
fi
fi
}
remove_path() {
local -r P=$1
local -r d=$2
local new_path=''
local -r _P=( ${!P//:/ } )
# loop over all entries in path
for entry in "${_P[@]}"; do
[[ "${entry}" != "${d}" ]] && new_path+=":${entry}"
done
# remove leading ':'
eval ${P}="${new_path:1}"
}
# Local Variables:
# mode: sh
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment