From df9adc825184bfbffd543c56a45ff513d1a6df1b Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Tue, 7 Apr 2015 15:26:06 +0200 Subject: [PATCH] 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 --- scripts/Bootstrap/Pmodules/libpmodules.bash | 70 ++++++++++++++++++--- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/scripts/Bootstrap/Pmodules/libpmodules.bash b/scripts/Bootstrap/Pmodules/libpmodules.bash index c54bb78..d3a05a0 100644 --- a/scripts/Bootstrap/Pmodules/libpmodules.bash +++ b/scripts/Bootstrap/Pmodules/libpmodules.bash @@ -1,5 +1,10 @@ #!/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