Test empty var in main

This commit is contained in:
Jeremy Gardais 2021-11-19 14:05:56 +01:00
parent 91eaa7642a
commit 6c134757cf
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 38 additions and 6 deletions

View File

@ -67,8 +67,8 @@ error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
## Print error message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
@ -77,21 +77,53 @@ define_vars() { # {{{
# If ldap_group_cn wasn't defined (argument) {{{
if [ -z "${ldap_group_cn}" ]; then
## Display help message
usage
## As ldap_group_cn is required, exit with message and error
error_message "Please enter a GROUP with -g|--group option" "1"
## Keep it empty
ldap_group_cn=""
fi
# }}}
}
# }}}
is_var_empty() { # {{{
## Return False by default
return_var_empty="1"
## Total number of variables to test
local_total_var="${#}"
loop_count_var_empty="0"
## While it remains a variable to test
while [ "${local_total_var}" -gt "${loop_count_var_empty}" ]; do
debug_message "is_var_empty \
Test var: ${1}."
### Test if this is empty and set return value to True
[ -z "${1}" ] && return_var_empty="0"
### Increase the number of tested variables
loop_count_var_empty=$((loop_count_var_empty+1))
### Shift to the next variable
shift
done
return "${return_var_empty}"
}
# }}}
main() { # {{{
## Define all vars
define_vars
## If ldap_group_cn is empty
### Print help message
### AND exit with message and error
is_var_empty "${ldap_group_cn}" \
&& usage \
&& error_message "Please enter a GROUP with -g|--group option." "1"
}
# }}}