Accept options

This commit is contained in:
Jeremy Gardais 2022-07-11 11:34:47 +02:00
parent 65767cfffd
commit a4c49e9f85
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 89 additions and 26 deletions

View File

@ -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