From 16e1324b2abb199af60191f8b362b697c0fb35c9 Mon Sep 17 00:00:00 2001 From: Gardouille Date: Wed, 15 Jul 2020 17:40:55 +0200 Subject: [PATCH] Set a main function --- winboot | 64 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/winboot b/winboot index 7944e29..36eee84 100755 --- a/winboot +++ b/winboot @@ -1,31 +1,47 @@ #!/bin/sh -GRUB_DEFAULT_PATH='/etc/default/grub' -GRUB_DEFAULT_DIR='/etc/default/grub.d' -WIN_GRUB="2" +# This script set a specific grub entry (winLOOSE…) for the next boot. +# Then reboot the host -# First check in Grub default dir if saved mode is set -if grep -q -E -R -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_DIR}" -then - printf '%b' "Reboot to windaube partition\\n" - sudo grub-reboot "${WIN_GRUB}" - sudo systemctl reboot -fi +# Vars {{{ +readonly PROGNAME=$(basename "${0}") +readonly PROGDIR=$(readlink -m $(dirname "${0}")) +readonly ARGS="${*}" +readonly NBARGS="${#}" -# Otherwise check the default grub file -if grep -q -E -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_PATH}" -then - printf '%b' "Reboot to windaube partition\\n" - sudo grub-reboot "${WIN_GRUB}" - sudo systemctl reboot -else +readonly GRUB_DEFAULT_PATH='/etc/default/grub' +readonly GRUB_DEFAULT_DIR='/etc/default/grub.d' +readonly WIN_GRUB="2" +# }}} - 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 once again.\\n" -fi +main() { # {{{ + + # First check in Grub default dir if saved mode is set + if grep -q -E -R -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_DIR}" + then + printf '%b' "Reboot to windaube partition\\n" + sudo grub-reboot "${WIN_GRUB}" + sudo systemctl reboot + fi + + # Otherwise check the default grub file + if grep -q -E -- "^GRUB_DEFAULT=saved" "${GRUB_DEFAULT_PATH}" + then + printf '%b' "Reboot to windaube partition\\n" + sudo grub-reboot "${WIN_GRUB}" + sudo systemctl reboot + 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 +} +# }}} + +main exit 0