When a function is a subroutine of another function, it is called _SomethingAsSubFunction
++++ Function argument check
Bash does not provide any checks against missing function arguments. Also, missing quotes can lead to an inconsistent number of arguments.
Every function call will be checked by __CheckArguments which takes the number of arguments, $# (the real number of args given), the parent function name and the parent function's arguments.
__CheckArguments will trigger a critical error if number of arguments if incorrect. This will also prevent silent typo errors.
Ex:
function Something {
local some="${1}"
local other="${2}"
local args="${3}"
__CheckArguments 3 $# $FUNCNAME "$*"
__CheckArguments will only trigger if script is called with DEBUG=yes
Also, with PARANOIA_DEBUG=yes, __CheckArguments will recount all arguments given by "$*" and compare. This can mislead if arguments contain spaces.
++++ If statements
If statements will be fully written (word "if" must be used). then is written on the same line.
Logger "Inconsistnent number of arguments in $function_name. Should have $number_of_arguments arguments, has $number_of_given_arguments arguments, see log file.""CRITICAL"
# Cannot user Logger here because $@ is a list of arguments
echo"Argumnt list: $4" >> "$LOG_FILE"
fi
if["$PARANOIA_DEBUG"=="yes"];then
# Paranoia check... Can help finding empty arguments
localcount=-3 # Number of arguments minus the function calls for __CheckArguments
for i in $@;do
count=$((count +1))
done
if[$count -ne $1];then
Logger "Function $function_name may have inconsistent number of arguments. Expected: $number_of_arguments, count: $count, see log file.""WARN"
echo"Argument list (including checks): $@" >> "$LOG_FILE"
localdeleted_list_file="${2}"# file containing deleted file list, will be prefixed with replica type
localdeletion_dir="${3}"# deletion dir in format .[workdir]/deleted
localdeleted_failed_list_file="${4}"# file containing files that couldn't be deleted on last run, will be prefixed with replica type
__CheckArguments 4"$#""$FUNCNAME""$*"
## On every run, check wheter the next item is already deleted because it's included in a directory already deleted
previous_file=""
@ -1190,6 +1204,7 @@ function _delete_remote {
localdeleted_list_file="${2}"# file containing deleted file list, will be prefixed with replica type
localdeletion_dir="${3}"# deletion dir in format .[workdir]/deleted
localdeleted_failed_list_file="${4}"# file containing files that couldn't be deleted on last run, will be prefixed with replica type
__CheckArguments 4"$#""$FUNCNAME""$*"
## This is a special coded function. Need to redelcare local functions on remote host, passing all needed variables as escaped arguments to ssh command.
## Anything beetween << ENDSSH and ENDSSH will be executed remotely