Manage debug, help and hostname options

This commit is contained in:
Jeremy Gardais 2020-06-24 14:07:23 +02:00
parent c730d36ed4
commit cc1ccba855
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 84 additions and 24 deletions

View File

@ -6,7 +6,7 @@ readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
## Test if DEBUG is already defined (by parent script,…)
[ -z "${DEBUG}" ] && readonly DEBUG=1
[ -z "${DEBUG}" ] && DEBUG=1
## Colors
readonly PURPLE='\033[1;35m'
@ -15,29 +15,10 @@ readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
manage_args() { # {{{
case "${NBARGS}" in
0 )
sge_hostname="$(hostname -f)"
;;
1 )
sge_hostname="${1}"
;;
* )
printf '%b\n' "${RED}Expect 1 or 0 argument.${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
}
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [hostname]
usage: $PROGNAME [--help] [-d|-h] [hostname]
Try to disable all SGE queues of the current host (default)
or the one passed as first argument.
@ -48,6 +29,17 @@ EXAMPLES:
- Disable SGE's queue(s) of "marvin.domain.tld" host
${PROGNAME} marvin.domain.tld
${PROGNAME} -h marvin.domain.tld
OPTIONS:
-d,--debug
Enable debug messages.
--help
Print this help message.
-h,--host,--hostname SGE_HOST_TO_MANAGE
Manage SGE's queue(s) of "SGE_HOST_TO_MANAGE" host.
EOF
}
@ -170,8 +162,6 @@ Try to disable SGE queue: ${RED}${local_sge_queue_name:=/dev/null}@${local_sge_h
# }}}
main() { # {{{
manage_args "${ARGS}"
sge_queues_name="$(qhost -h "${sge_hostname:=/dev/null}" -q -xml \
| grep "queue name" \
| cut -d"'" -f2 )"
@ -193,6 +183,10 @@ for host: ${RED}${sge_hostname:=/dev/null}${COLOR_DEBUG}."
## Test all queues one by one
for loop_queue in ${sge_queues_name}; do
## File that says this queue was disabled by a script
## Can be used by other script to verify the queue wasn't disable by a user/admin
sge_queue_deactivator="/etc/.sge.${loop_queue}.disable"
## If the queue is enable
### Try to disable it
is_queue_enable "${sge_hostname}" "${loop_queue}" \
@ -209,6 +203,72 @@ for host: ${RED}${sge_hostname:=/dev/null}${COLOR_DEBUG}."
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to arguments
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Use this argument for sge_hostname
sge_hostname="${1}"
## Switch to the next argument
shift
manage_arg=$((manage_arg+1))
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
-d|--debug ) ## debug
DEBUG=0
;;
--help ) ## help
usage
## Exit after help informations
exit 0
;;
-h|--host|--hostname ) ## Specify a different host to manage
## Move to the next argument
shift
## Override previous definition of sge_hostname
sge_hostname="${1}"
;;
-- ) ## End of options list
## End the while loop
break
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0