Exit on failure and refactoring

This commit is contained in:
Jeremy Gardais 2020-07-15 18:27:15 +02:00
parent 4da7cdad05
commit 41acad009e
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 24 additions and 17 deletions

41
winboot
View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# This script set a specific grub entry (winLOOSE…) for the next boot. # This script set a specific grub entry (winLOOSE…) for the next boot.
# Then reboot the host # Then reboot the host (by default) or just poweroff (if requested).
# Vars {{{ # Vars {{{
readonly PROGNAME=$(basename "${0}") readonly PROGNAME=$(basename "${0}")
@ -74,28 +74,35 @@ main() { # {{{
# First check in Grub default dir if saved mode is set # First check in Grub default dir if saved mode is set
if grep -q -E -R -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_DIR}" if grep -q -E -R -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_DIR}"
then then
printf '%b' "Reboot to windaube partition\\n" debug_message "saved mode is enable in ${GRUB_DEFAULT_DIR} dir"
sudo grub-reboot "${WIN_GRUB}"
[ "${REBOOT}" -eq "0" ] && sudo systemctl reboot
[ "${HALT}" -eq "0" ] && sudo systemctl poweroff
fi
# Otherwise check the default grub file # Second check the default grub file
if grep -q -E -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_PATH}" elif grep -q -E -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_PATH}"
then then
debug_message "saved mode is enable in ${GRUB_DEFAULT_PATH} file"
# Otherwise enable saved mode
else
printf '%b' "GRUB_DEFAULT is not set in 'saved' mode\n"
debug_message "Enable grub saved mode in ${GRUB_DEFAULT_PATH} file"
sudo sed -i 's/\(^GRUB_DEFAULT.*\)/#\1\nGRUB_DEFAULT=saved/' "${GRUB_DEFAULT_PATH}"
GRUB_DEFAULT_ENTRY=$(grep -E -- "#GRUB_DEFAULT=" "${GRUB_DEFAULT_PATH}" | cut -d"=" -f2)
## Set current GRUB_DEFAULT as default grub's entry
## Exit if fails
sudo grub-set-default "${GRUB_DEFAULT_ENTRY}" \
|| exit 1
## Try to update grub's configuration
## Exit if fails
sudo update-grub \
|| exit 2
fi
printf '%b' "Reboot to windaube partition\\n" printf '%b' "Reboot to windaube partition\\n"
sudo grub-reboot "${WIN_GRUB}" sudo grub-reboot "${WIN_GRUB}"
[ "${REBOOT}" -eq "0" ] && sudo systemctl reboot [ "${REBOOT}" -eq "0" ] && sudo systemctl reboot
[ "${HALT}" -eq "0" ] && sudo systemctl poweroff [ "${HALT}" -eq "0" ] && sudo systemctl poweroff
else
printf '%b' "GRUB_DEFAULT is not set in 'saved' mode\\n"
sudo sed -i 's/\(^GRUB_DEFAULT.*\)/#\1\nGRUB_DEFAULT=saved/' "${GRUB_DEFAULT_PATH}"
GRUB_DEFAULT_ENTRY=$(grep -E -- "#GRUB_DEFAULT=" "${GRUB_DEFAULT_PATH}" | cut -d"=" -f2)
sudo grub-set-default "${GRUB_DEFAULT_ENTRY}"
sudo update-grub
printf '%b' "Please launch this script (${PROGDIR}/${PROGNAME} ${ARGS}) once again.\\n"
fi
} }
# }}} # }}}