2 ##########################################################################
6 # This file is part of rootpwa
8 # rootpwa is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # rootpwa is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
21 ##########################################################################
22 #-------------------------------------------------------------------------
23 # File and Version Information:
24 # $Rev:: $: revision of last commit
25 # $Author:: $: author of last commit
26 # $Date:: $: date of last commit
29 # collection of useful bash functions
33 # Boris Grube TUM (original author)
36 #-------------------------------------------------------------------------
39 # converts relative into absolute pathnames
40 # usage: pathRel2Abs "${RELATIVE_PATH}" ABSOLUTE_PATH
43 local _ABSOLUTE_PATH=$(readlink --canonicalize-missing ${1})
44 eval
"$2=${_ABSOLUTE_PATH}"
48 # prepends
a path to path list stored in an environment variable making
49 # sure not to
double entries; seperator
':' is assumed
50 # usage: prependPathToEnvVar VARIABLE "${PATH}"
56 if eval
"test -z \$${_VARIABLE}"
58 export ${_VARIABLE}=${_PATH}
60 local _FOUND=$(eval
echo \$${_VARIABLE} | tr ${_SEPARATOR} \\n | grep ^${_PATH}\$)
61 if [[ -z
"${_FOUND}" ]]
63 export ${_VARIABLE}=$(eval
echo ${_PATH}${_SEPARATOR}\$${_VARIABLE})
69 # appends
a path to path list stored in an environment variable making
70 # sure not to
double entries; seperator
':' is assumed
71 # usage: appendPathToEnvVar VARIABLE "${PATH}"
77 if eval
"test -z \$${_VARIABLE}"
79 export ${_VARIABLE}=${_PATH}
81 local _FOUND=$(eval
echo \$${_VARIABLE} | tr ${_SEPARATOR} \\n | grep ^${_PATH}\$)
82 if [[ -z
"${_FOUND}" ]]
84 export ${_VARIABLE}=$(eval
echo \$${_VARIABLE}${_SEPARATOR}${_PATH})
90 # waits until number of concurrently running processes is below threshold
91 #
usage:
waitForJobs "${JOB_PATTERN}" [
"${MAX_NMB_JOBS}"] [
"${POLL_INTERVAL}"] [
"${GUARD_INTERVAL}"]
94 local _JOB_PATTERN=${1}
95 local _MAX_NMB_JOBS=${2:-3}
96 local _POLL_INTERVAL=${3:-10s}
97 local _GUARD_INTERVAL=${4:-5s}
98 sleep ${_GUARD_INTERVAL} # give job some
time to show up in process list
102 # get number of running jobs
103 declare -
i _NUM_JOBS=$(pgrep -f
"${_JOB_PATTERN}" | wc -l)
104 # wait until number of jobs is smaller than maximum allowed
105 if ((
"${_NUM_JOBS}" <
"${_MAX_NMB_JOBS}" ))
109 sleep ${_POLL_INTERVAL}
115 # checks whether variable content is number
116 # usage: isNumber "${VARIABLE}"
119 if [ ${1} -eq ${1} 2> /dev/null ]
128 # converts string(s) passed as argument(s) to lower case
129 # usage: newvar=$( toLower "${oldVar}" )
132 if [ -z
"${1}" ] # no argument
136 # lower case all arguments
137 echo "$@" | tr
'[:upper:]' '[:lower:]'
142 # converts string(s) passed as argument(s) to upper case
143 # usage: newvar=$( toUpper "${oldVar}" )
146 if [ -z
"${1}" ] # no argument
150 # upper case all arguments
151 echo "$@" | tr
'[:lower:]' '[:upper:]'
156 # extracts value from text file
157 # usage: getFieldValue "${FILE_NAME}" "${VALUE_STRING}" VALUE
160 local _FILE_NAME=${1}
161 local _VALUE_STRING=${2}
162 if [[
"${_FILE_NAME##*.}" ==
"gz" ]]
168 local _VALUE_LINE=$(${GREP}
"${_VALUE_STRING}" ${_FILE_NAME})
169 if [[ -z
"${_VALUE_LINE}" ]]
173 local _VALUE=${_VALUE_LINE#*${_VALUE_STRING}}
174 # make sure _VALUE is number
175 if [ ! ${_VALUE} -eq ${_VALUE} 2> /dev/null ]
177 echo " value for '${_VALUE_STRING}' in '${_FILE_NAME}' is not an integer: ${_VALUE}"
180 # deference third argument