3 # Common utilities for the scripts in this directory.
6 # ------------------------------------------------------------------------------
9 [[ -
n "${!VarName//0}" ]]
14 [[ -
z "${!VarName//0}" ]]
17 function
STDERR() { echo
"$*" >&2 ; }
22 STDERR "FATAL (${Code}): $*"
27 [[
"$Code" == 0 ]] ||
FATAL "$Code" "$@"
31 # ------------------------------------------------------------------------------
34 [[
"${Path:0:1}" !=
'/' ]]
39 # Replaces '.' and '..' path components by plain text substitution.
40 local Path=
"$(sed -E 's@/+@/@g' <<< "${1}/
")" # replace
double / with single ones
42 # build a stack of directories, from the root on
45 local -i nParents=0 # number of parent directories at
root
49 OtherPath=
"${Path#${Dir}/}"
54 local -i lastElem=$((${#Stack[@]} - 1))
55 if [[ $lastElem -ge 0 ]] && [[
"${Stack[$lastElem]}" !=
'..' ]]; then
56 # if the element is empty then it's root (should be lastElem=0)
57 # and we can't escalate to the parent
58 [[ -
n "${Stack[$lastElem]}" ]] && unset Stack[$lastElem]
67 [[
"${Dir}/" ==
"$Path" ]] &&
break
71 # merge the elements left
72 local ReconstitutedPath
73 while [[ $nParents -gt 0 ]];
do
74 ReconstitutedPath+=
'../'
77 for Elem
in "${Stack[@]}" ;
do
78 ReconstitutedPath+=
"${Elem}/"
81 [[
"$ReconstitutedPath" !=
'/' ]] && ReconstitutedPath=
"${ReconstitutedPath%/}"
82 echo
"$ReconstitutedPath"
87 function makeAbsolutePath() {
92 [[ -
z "$RelativeTo" ]] && RelativeTo=
"$(pwd)"
93 Path=
"${RelativeTo%/}/${Path}"
97 } # makeAbsolutePath()
100 # ------------------------------------------------------------------------------
101 function isExperiment() {
103 # Prints the name of the experiment as deduced.
104 # Returns 0 on success, 1 if deduction failed, and 2 if more than one
108 local -ar ExperimentNames=(
'MicroBooNE' 'DUNE' 'SBND' 'LArIAT' 'ArgoNeuT' 'ICARUS' )
109 local -
A ExperimentHostNames ExptDirs
111 for Name in "${ExperimentNames[@]}" ;
do
112 ExperimentHostNames[$Name]=
"${Name,,}"
113 ExperimentDirectories[$Name]=
"${Name,,}"
115 ExperimentHostNames[
'MicroBooNE']=
'uboone'
116 ExperimentDirectories[
'MicroBooNE']=
'uboone'
119 local ExperimentCandidate ExperimentHostName
120 local HostName=
"$(hostname)"
121 if [[ -
n "$HostName" ]]; then
122 for ExperimentCandidate
in "${!ExperimentHostNames[@]}" ;
do
123 [[
"$HostName" =~ ^${ExperimentHostNames[$ExperimentCandidate]} ]] ||
continue
124 Experiments+=(
"$ExperimentCandidate" )
129 # back up to
directory presence
for known experiments
130 if [[
"${#Experiments[@]}" == 0 ]]; then
132 for ExperimentCandidate in
"${!ExperimentHostNames[@]}" ;
do
133 ExperimentDir=
"/${ExperimentDirectories[$ExperimentCandidate]}"
134 [[ -d
"$ExperimentDir" ]] && Experiments+=(
"$ExperimentCandidate" )
139 if [[
"${#Experiments[@]}" == 0 ]]; then
439 =/
' | sed -e 's/[[:blank:]]*#.*$
441 [[ $res != 0 ]] &&
return $res
442 [[ -
z "$DefLine" ]] &&
return 1
444 local "$DefLine" ||
FATAL 1
"Could not assign '${Key}' with the line \`local \"${DefLine}\"\`."
449 } # ExtractFromMetadata()
452 function ExtractValueFromDoxyfile() {
454 # Prints the value assigned to VarName in the specified file.
455 # An error code (1) is returned if the variable is not defined.
457 # Usage: ExtractValueFromDoxyfile VarName DefinitionFile
459 # This works only for variables with values defined on a single line.
460 # Also, it does not do any decent parsing.
465 # since our metadata has a format compatible with Doxygen, we use the same tool
466 ExtractFromMetadata
"$VarName" "$Doxyfile"
468 } # ExtractValueFromDoxyfile()
471 # ------------------------------------------------------------------------------
472 function FindFirstVariableName() {
474 # Prints the name of the first "variable" in the input stream, introduced as
475 # "${VarName}", with VarName a valid bash variable identifier
480 # * do not print anything by default
481 # * look for a line with pattern ${VarName} (VarName made of letters, underscores and, except the first character, numbers
482 # * when found, execute the three commands:
483 # * match the variable name and replace the whole line with it
484 # * print the new line
487 sed -
n -
E '/\$\{[[:alpha:]_][[:alnum:]_]*\}/{s/^.*\$\{([[:alpha:]_][[:alnum:]_]*)\}.*$/\1/;p;q}'
488 } # FindFirstVariableName()
491 function EscapeSedReplacementLiteralPattern() {
494 Value=
"${Value//\\/\\\\}"
495 [[
"$Escape" !=
'\' ]] && Value=
"${Value//${Escape}/\\${Escape}}"
497 } # EscapeSedReplacementLiteralPattern()
500 function ReplaceEnvironmentVariables() {
502 # Using `sed`, which means a lot of potential problems...
504 local -
r TemplateFile=
"$1"
507 if [[
"$TemplateFile" -ef
"$DestFile" ]]; then
508 FATAL 1
"ReplaceEnvironmentVariables does not support in-place changes ('${TemplateFile}' and '${DestFile}' are the same file)."
511 cp
"$TemplateFile" "$DestFile"
512 LASTFATAL "ReplaceEnvironmentVariables: can't create file '${DestFile}'."
514 local VarName LastVarName
516 VarName=
"$(FindFirstVariableName < "$DestFile
")"
517 [[ $? == 0 ]] ||
break # done
518 [[ -
z "$VarName" ]] &&
break # done
520 # avoid infinite loops (from bugs, presumably)
521 [[
"$LastVarName" ==
"$VarName" ]] &&
FATAL 1
"Internal error: variable '${VarName}' still present in '${DestFile}' after substitution."
522 LastVarName=
"$VarName"
526 LASTFATAL "ReplaceEnvironmentVariables: error evaluating the variable '${VarName}'."
528 echo
"Replacing: '\${${VarName}}' => '${Value}'"
529 sed -i -
E "s@\\$\\{${VarName}\\}@$(EscapeSedReplacementLiteralPattern "$Value
" '@')@g" "$DestFile"
530 LASTFATAL "ReplaceEnvironmentVariables: error replacing variable '${VarName}' with value '${Value}'."
534 } # ReplaceEnvironmentVariables()
537 # ------------------------------------------------------------------------------
538 # for test: bash utilities.sh testCommand args
539 if [[
"${BASH_SOURCE[0]}" ==
"$0" ]] && [[ $# -gt 0 ]]; then
546 # ------------------------------------------------------------------------------
process_name opflash particleana ie ie ie z
function isRelativePath()
BEGIN_PROLOG dataFFTHistosEW root
BEGIN_PROLOG vertical distance to the surface Name
then echo Work directory not specified exit fi echo Work directory
if &&[-z"$BASH_VERSION"] then echo Attempting to switch to bash bash shellSwitch exit fi &&["$1"= 'shellSwitch'] shift declare a IncludeDirectives for Dir in
then echo File list $list not found else cat $list while read file do echo $file sed s
BEGIN_PROLOG don t mess with this pandoraTrackGausCryoW true