6 #--------------------------------------------------------------------------------------
8 # Utility script for zsh library functions.
10 #--------------------------------------------------------------------------------------
14 if [[ "$DEBUG" == "" ]]; then
15 typeset -i DEBUG=0 # debug level
19 typeset -i TIMER=0 # timer
22 export DEFAULT_OPTION=- # default option
25 # Wild card for any valid run number to be used as ${~ANY_RUN_NUMBER}.
27 export ANY_RUN_NUMBER="[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
29 export PI=3.14159265359
37 export TIMESTAMP=\#splitline{}{\#splitline{%d:%m:%y}{\ \ %H:%M}}%F1970-01-01\ 00:00:00
44 echo -e $RED`date` FATAL $* $RESET
47 function error() { if (( $DEBUG >= 0 )); then echo -e $RED`date` ERROR $* $RESET; fi }
48 function notice() { if (( $DEBUG >= 1 )); then echo -e `date` NOTICE $* ; fi }
49 function status() { if (( $DEBUG >= 2 )); then echo -e `date` STATUS $* ; fi }
50 function warning() { if (( $DEBUG >= 2 )); then echo -e `date` WARNING $* ; fi }
51 function debug() { if (( $DEBUG >= 3 )); then echo -e `date` DEBUG $* ; fi }
55 # Method to check for usage of script.
57 # \param 1-N list of command line options
58 # \return 0 for option -h; else 1
62 for variable in $*; do
63 if [[ "$variable" == "-h" || "$variable" == "-h!" ]]; then
73 # Method to print usage of script and exit.
85 # Method to print environment variables.
95 # The last parameter refers to the exit code.
100 function check_exit_code()
102 if (( $argv[-1] )); then
114 # \param 1 first value
115 # \param 2 second value
117 function test_equality
119 if [[ "$1" != "$2" ]]; then
128 # check exit code alias
130 alias CHECK_EXIT_CODE="check_exit_code \$0 \$* \$?"
134 # Method to check if inside a conda env.
138 if [ -n "${CONDA_PREFIX+1}" ]; then
147 # Method to check for CC Lyon.
149 # \return 1 for CC Lyon; else 0
155 if [[ -n "${LOCALHOST}" && "${LOCALHOST:0:2}" = "cc" ]]; then
164 # Method to get file name of source code.
166 # \param 1 executable file
167 # \return source file
169 function get_source()
171 file=`which $1 2> /dev/null`
173 if [[ -n "${file}" && -x ${file} ]]; then
174 if [[ $file = *.sh || $file = *.csh ]]; then
177 source=`${file} -v 2> /dev/null | sed -n 's/source: *//p'`
188 # Method to define variable.
192 function define_variable()
196 eval export $variable
201 # Method to set variable.
202 # Note that a value equal to $DEFAULT_OPTION will not modify the variable.
205 # \param 2-N value(s)
207 function set_variable()
212 if [[ "$*" != "$DEFAULT_OPTION" ]]; then
213 eval export $variable=\"$\*\"
219 # Method to unset variable.
223 function unset_variable()
232 # Method to print variables.
234 # \param 1-N list of variables
236 function print_variable()
238 for variable in $*; do
239 eval value=\${$variable}
240 printf "%-20s = %s\n" ${variable} "${value}"
246 # Method to check validity of variables.
248 # \param 1-N list of variables
250 function check_variable()
252 for variable in $*; do
253 eval value=$+${variable}
254 if (( ${value} == 0 )); then
255 fatal "Variable ${variable} not defined."
262 # Method to set array.
264 # \param 1 array name
272 eval $variable=\($\*\)
277 # Method to count directory in ':' separated path list.
282 function count_directory()
286 echo -n $key | tr ":" "\n" | grep "^\\${2}$" | wc -w
291 # Method to remove directory from ':' separated path list.
296 function remove_directory()
301 if [[ -n "$key" && -n "$value" ]]; then
302 export $1=`eval echo $key | tr ":" "\n" | grep -v "^\\\\${value}$" | tr "\n" ":" | sed 's/:\$//'`
308 # Method to remove variable from ':' separated path list.
313 function remove_variable()
318 if [[ -n "$key" && -n "$value" ]]; then
319 remove_directory $1 $value
325 # Method to insert directory into ':' separated path list.
330 function insert_directory()
334 if [[ -z "$key" ]]; then
336 elif (( `eval echo -n $key | tr ":" "\n" | grep "^\\\\${2}$" | wc -w` == 0 )); then
343 # Method to check process path.
345 # \param 1-N list of process names
347 function check_process()
349 for process in $*; do
351 local bin=`which $process 2>& /dev/null`
352 debug "Check process ${process}=${bin}"
354 if [ -z "${bin}" ]; then
355 fatal "Process ${process} not found."
362 # Method to check file access.
364 # \param 1-N list of files
366 function check_input_file()
368 if (( $# == 0 )); then
369 fatal "No input file."
372 for file in `echo $*`; do
374 debug "Check input file: ${file}"
376 if [[ ! -f ${file} ]]; then
377 fatal "File ${file} not found."
378 elif [[ ! -r ${file} ]]; then
379 fatal "File ${file} not readable."
386 # Method to check file access.
388 # \param 1-N list of files
390 function check_output_file()
392 for file in `echo $*`; do
394 debug "Check output file: ${file}"
396 if [[ -f ${file} ]]; then
397 fatal "File ${file} already exists."
404 # Method to reuse file.
405 # Note that this method may require user interaction.
408 # \return 1 when absent; else 0
410 function reuse_file()
416 if [[ ! -f $1 ]]; then
425 # Get host IP address
427 function get_ip_address()
429 hostname --all-ip-addresses | cut --delimiter=' ' -f1
434 # Get process identifier.
437 # \param process name
442 if (( $# == 1 )); then
443 exec ps h -o "%p" -C $1
444 elif (( $# == 2 )); then
445 exec ssh $1 "ps h -o "%p" -C $2"
451 # Method to get least significant digit.
458 printf "%f\n" `echo $1 | sed -n "s/[+-]\?\([0-9]*.\?[0-9]*\)[1-9]\([0]*.\?\)/\1A\2/p" | sed 's/[1-9]/0/g' | sed 's/A/1/'`
463 # Method to start timer
465 function timer_start()
472 # Method to stop timer
474 function timer_stop()
476 TIMER=$(($(date +%s) - $TIMER))
481 # Method to print timer
483 function timer_print()
485 echo "Elapsed time $TIMER s."
490 # Make communication with process.
493 # - FD_O is the actual file descriptor for writing to actual process and
494 # - FD_I is the actual file descriptor for reading from process.
496 # \param 1 process name
497 # \param 2-N optional argument(s)
505 ARGS="$*" # optional arguments
507 FIFO_O=/tmp/${PROCESS}_writing # named pipe for writing
508 FIFO_I=/tmp/${PROCESS}_reading # named pipe for reading
513 mkfifo $FIFO_O # create a new FIFO
514 mkfifo $FIFO_I # create a new FIFO
516 eval "exec {FD_O}<>$FIFO_O" # attach file descriptor to FIFO
517 eval "exec {FD_I}<>$FIFO_I" # attach file descriptor to FIFO
519 # launch process in background and cleanup afterwards
531 # Remove communication with process.
533 # \param 1 file descriptor for writing to process
537 if (( $# == 0 )); then