Add options management

This commit is contained in:
Jeremy Gardais 2020-06-22 13:48:26 +02:00
parent 3d01cb8487
commit 2e493e086f
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 76 additions and 27 deletions

View File

@ -6,7 +6,7 @@ readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}" readonly ARGS="${*}"
readonly NBARGS="${#}" readonly NBARGS="${#}"
## Test if DEBUG is already defined (by parent script,…) ## Test if DEBUG is already defined (by parent script,…)
[ -z "${DEBUG}" ] && readonly DEBUG=0 [ -z "${DEBUG}" ] && DEBUG=1
# Maco temp file # Maco temp file
readonly MACO_TMP_FILE="/tmp/.maco.upgrade" readonly MACO_TMP_FILE="/tmp/.maco.upgrade"
@ -15,6 +15,9 @@ readonly MACO_TMP_URGENT_FILE="/tmp/.maco.urgent.upgrade"
# APT temp file to monitor # APT temp file to monitor
readonly APT_TMP_FILE="/tmp/.apt.upgrade" readonly APT_TMP_FILE="/tmp/.apt.upgrade"
# If the scrip need to check pending upgrade before enable a queue
CHECK_UPGRADE="0"
## Colors ## Colors
readonly PURPLE='\033[1;35m' readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m' readonly RED='\033[0;31m'
@ -22,33 +25,10 @@ readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}" readonly COLOR_DEBUG="${PURPLE}"
# }}} # }}}
manage_args() { # {{{
case "${NBARGS}" in
0 )
sge_hostname="$(hostname -f)"
## Ensure there is no pending upgrades on the local host
CHECK_UPGRADE="0"
;;
1 )
sge_hostname="${1}"
## No way to monitor pending upgrades on a remote host
CHECK_UPGRADE="1"
;;
* )
printf '%b\n' "${RED}Expect 1 or 0 argument.${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
}
# }}}
usage() { # {{{ usage() { # {{{
cat <<- EOF cat <<- EOF
usage: $PROGNAME [hostname] usage: $PROGNAME [--help|-d,--debug] [hostname]
Try to enable all SGE queues of the current host (default) Try to enable all SGE queues of the current host (default)
or the one passed as first argument. or the one passed as first argument.
@ -238,8 +218,6 @@ Try to enable SGE queue: ${RED}${local_sge_queue_name:=/dev/null}@${local_sge_ho
# }}} # }}}
main() { # {{{ main() { # {{{
manage_args "${ARGS}"
sge_queues_name="$(qhost -h "${sge_hostname:=/dev/null}" -q -xml \ sge_queues_name="$(qhost -h "${sge_hostname:=/dev/null}" -q -xml \
| grep "queue name" \ | grep "queue name" \
| cut -d"'" -f2 )" | cut -d"'" -f2 )"
@ -289,6 +267,77 @@ 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}"
## No way to monitor pending upgrades on a remote host
CHECK_UPGRADE="1"
## Switch to next arg
shift
manage_arg=$((manage_arg+1))
else
## Use local host for sge_hostname
sge_hostname="$(hostname -f)"
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-*"; do
case "${1}" in
--help ) ## help
usage
## Exit after help informations
exit 0
;;
-d|--debug ) ## debug
DEBUG=0
## Re-export new DEBUG value
export DEBUG
;;
-- ) ## 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."
## Next arg
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."
## Use local host for sge_hostname
sge_hostname="$(hostname -f)"
fi
# }}}
main main
exit 0 exit 0