From ecab3bea7e39a5304dde0282f09b0bc433fd7cff Mon Sep 17 00:00:00 2001 From: Gardouille Date: Wed, 15 Jul 2020 17:48:09 +0200 Subject: [PATCH] Add argument management --- winboot | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/winboot b/winboot index 36eee84..e7da479 100755 --- a/winboot +++ b/winboot @@ -12,6 +12,10 @@ readonly NBARGS="${#}" readonly GRUB_DEFAULT_PATH='/etc/default/grub' readonly GRUB_DEFAULT_DIR='/etc/default/grub.d' readonly WIN_GRUB="2" + +## By default, the host should reboot at the end of the script +REBOOT=0 + # }}} main() { # {{{ @@ -21,7 +25,7 @@ main() { # {{{ then printf '%b' "Reboot to windaube partition\\n" sudo grub-reboot "${WIN_GRUB}" - sudo systemctl reboot + [ "${REBOOT}" -eq "0" ] && sudo systemctl reboot fi # Otherwise check the default grub file @@ -29,7 +33,7 @@ main() { # {{{ then printf '%b' "Reboot to windaube partition\\n" sudo grub-reboot "${WIN_GRUB}" - sudo systemctl reboot + [ "${REBOOT}" -eq "0" ] && sudo systemctl reboot else printf '%b' "GRUB_DEFAULT is not set in 'saved' mode\\n" @@ -42,6 +46,42 @@ main() { # {{{ } # }}} +# Manage arguments # {{{ +# This code can't be in a function due to arguments + +if [ ! "${NBARGS}" -eq "0" ]; then + + manage_arg="0" + + # Parse all options (start with a "-") one by one + while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do + + case "${1}" in + --reboot ) ## A reboot was requested (default behaviour) + REBOOT=0 + ;; + -- ) ## 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 + + ## Move to the next argument + shift + manage_arg=$((manage_arg+1)) + + done + +fi + +# }}} + main exit 0