ROOTPWA
batchUtils.sh
Go to the documentation of this file.
1 #!/bin/bash
2 ##########################################################################
3 #
4 # Copyright 2010
5 #
6 # This file is part of rootpwa
7 #
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.
12 #
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.
17 #
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/>.
20 #
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
27 #
28 # Description:
29 # collection of useful bash functions
30 #
31 #
32 # Author List:
33 # Boris Grube TUM (original author)
34 #
35 #
36 #-------------------------------------------------------------------------
37 
38 
39 # converts relative into absolute pathnames
40 # usage: pathRel2Abs "${RELATIVE_PATH}" ABSOLUTE_PATH
41 function pathRel2Abs ()
42 {
43  local _ABSOLUTE_PATH=$(readlink --canonicalize-missing ${1})
44  eval "$2=${_ABSOLUTE_PATH}"
45 }
46 
47 
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}"
52 {
53  local _PATH=${2}
54  local _VARIABLE=${1}
55  local _SEPARATOR=":"
56  if eval "test -z \$${_VARIABLE}"
57  then
58  export ${_VARIABLE}=${_PATH}
59  else
60  local _FOUND=$(eval echo \$${_VARIABLE} | tr ${_SEPARATOR} \\n | grep ^${_PATH}\$)
61  if [[ -z "${_FOUND}" ]]
62  then
63  export ${_VARIABLE}=$(eval echo ${_PATH}${_SEPARATOR}\$${_VARIABLE})
64  fi
65  fi
66 }
67 
68 
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}"
72 function appendPathToEnvVar ()
73 {
74  local _PATH=${2}
75  local _VARIABLE=${1}
76  local _SEPARATOR=":"
77  if eval "test -z \$${_VARIABLE}"
78  then
79  export ${_VARIABLE}=${_PATH}
80  else
81  local _FOUND=$(eval echo \$${_VARIABLE} | tr ${_SEPARATOR} \\n | grep ^${_PATH}\$)
82  if [[ -z "${_FOUND}" ]]
83  then
84  export ${_VARIABLE}=$(eval echo \$${_VARIABLE}${_SEPARATOR}${_PATH})
85  fi
86  fi
87 }
88 
89 
90 # waits until number of concurrently running processes is below threshold
91 # usage: waitForJobs "${JOB_PATTERN}" ["${MAX_NMB_JOBS}"] ["${POLL_INTERVAL}"] ["${GUARD_INTERVAL}"]
92 function waitForJobs ()
93 {
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
99  # infinite loop
100  while true
101  do
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}" ))
106  then
107  break
108  else
109  sleep ${_POLL_INTERVAL}
110  fi
111  done
112 }
113 
114 
115 # checks whether variable content is number
116 # usage: isNumber "${VARIABLE}"
117 function isNumber ()
118 {
119  if [ ${1} -eq ${1} 2> /dev/null ]
120  then
121  return 0
122  else
123  return 1
124  fi
125 }
126 
127 
128 # converts string(s) passed as argument(s) to lower case
129 # usage: newvar=$( toLower "${oldVar}" )
130 function toLower ()
131 {
132  if [ -z "${1}" ] # no argument
133  then
134  return
135  fi
136  # lower case all arguments
137  echo "$@" | tr '[:upper:]' '[:lower:]'
138  return
139 }
140 
141 
142 # converts string(s) passed as argument(s) to upper case
143 # usage: newvar=$( toUpper "${oldVar}" )
144 function toUpper ()
145 {
146  if [ -z "${1}" ] # no argument
147  then
148  return
149  fi
150  # upper case all arguments
151  echo "$@" | tr '[:lower:]' '[:upper:]'
152  return
153 }
154 
155 
156 # extracts value from text file
157 # usage: getFieldValue "${FILE_NAME}" "${VALUE_STRING}" VALUE
158 function getFieldValue ()
159 {
160  local _FILE_NAME=${1}
161  local _VALUE_STRING=${2}
162  if [[ "${_FILE_NAME##*.}" == "gz" ]]
163  then
164  GREP="zgrep"
165  else
166  GREP="grep"
167  fi
168  local _VALUE_LINE=$(${GREP} "${_VALUE_STRING}" ${_FILE_NAME})
169  if [[ -z "${_VALUE_LINE}" ]]
170  then
171  return 1
172  else
173  local _VALUE=${_VALUE_LINE#*${_VALUE_STRING}}
174  # make sure _VALUE is number
175  if [ ! ${_VALUE} -eq ${_VALUE} 2> /dev/null ]
176  then
177  echo " value for '${_VALUE_STRING}' in '${_FILE_NAME}' is not an integer: ${_VALUE}"
178  fi
179  fi
180  # deference third argument
181  eval "$3=${_VALUE}"
182  return 0
183 }