From a4c49e9f859bf41db83f01f4748361b19f9905f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Mon, 11 Jul 2022 11:34:47 +0200 Subject: [PATCH] Accept options --- cluster/apt.apply.update.sh | 115 ++++++++++++++++++++++++++++-------- 1 file changed, 89 insertions(+), 26 deletions(-) diff --git a/cluster/apt.apply.update.sh b/cluster/apt.apply.update.sh index 49f93bf..3eabdbe 100755 --- a/cluster/apt.apply.update.sh +++ b/cluster/apt.apply.update.sh @@ -12,7 +12,7 @@ readonly PROGNAME=$(basename "${0}") readonly PROGDIR=$(readlink -m $(dirname "${0}")) readonly ARGS="${*}" readonly NBARGS="${#}" -[ -z "${DEBUG}" ] && readonly DEBUG=1 +[ -z "${DEBUG}" ] && DEBUG=1 ## Export DEBUG for sub-script export DEBUG @@ -26,25 +26,10 @@ readonly RESET='\033[0m' readonly COLOR_DEBUG="${PURPLE}" # }}} -manage_args() { # {{{ - - case "${NBARGS}" in - 0 ) ## Nothing to do - ;; - * ) - printf '%b\n' "${RED}Don't expect any arguments.${RESET}" - printf '%b\n' "---" - usage - exit 1 - ;; - esac - -} -# }}} usage() { # {{{ cat <<- EOF -usage: $PROGNAME +usage: $PROGNAME [-d|-h] Apply any APT package upgrade if the host is free: * All SGE queues are disable @@ -54,6 +39,13 @@ Apply any APT package upgrade if the host is free: EXAMPLES: - Apply upgrade on the current host ${PROGNAME} + +OPTIONS : + -d,--debug + Enable debug messages. + + -h,--help + Print this help message. EOF } @@ -68,6 +60,28 @@ debug_message() { # {{{ return 0 } # }}} +define_vars() { # {{{ + + ## If sge_hostname wasn't defined (environment variable,…) {{{ + if [ -z "${sge_hostname}" ]; then + ## Use local host for sge_hostname + sge_hostname="$(hostname -f)" + fi + ## }}} + + ## If sge_queues_name wasn't defined (environment variable,…) {{{ + if [ -z "${sge_queues_name}" ]; then + ## Get queues from qhost + sge_queues_name="$(qhost -h "${sge_hostname:=/dev/null}" -q -xml \ + | grep "queue name" \ + | cut -d"'" -f2 )" + fi + ## }}} + + ## Process pattern to monitor + maco_proc_pattern="(/opt/maco/bin/maco.autoupdate.sh)" +} +# }}} is_sge_host() { # {{{ ## Check if SGE commands (qconf) are available @@ -318,15 +332,8 @@ main() { # {{{ || exit 0 ## }}} - manage_args "${ARGS}" - - ## Define vars - sge_hostname="$(hostname -f)" - sge_queues_name="$(qhost -h "${sge_hostname:=/dev/null}" -q -xml \ - | grep "queue name" \ - | cut -d"'" -f2 )" - - maco_proc_pattern="(/opt/maco/bin/maco.autoupdate.sh)" + ## Define all vars + define_vars ## If NO APT package upgrade is available ### Exit @@ -365,6 +372,62 @@ main() { # {{{ } # }}} +# Manage arguments # {{{ +# This code can't be in a function due to argument management + +if [ ! "${NBARGS}" -eq "0" ]; then + + manage_arg="0" + + ## If the first argument is not an option + if ! printf -- '%s' "${1}" | grep -q -E -- "^-+"; + then + ## Print help message and exit + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + + exit 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 + ;; + -h|--help ) ## help + usage + ## Exit after help informations + exit 0 + ;; + * ) ## 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 255