Skip to content

Instantly share code, notes, and snippets.

@HackingGate
Created April 20, 2025 14:19
Show Gist options
  • Save HackingGate/ce56647c87fc8a4131cd8e9b67615966 to your computer and use it in GitHub Desktop.
Save HackingGate/ce56647c87fc8a4131cd8e9b67615966 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
CFG="/boot/grub/grub.cfg"
# 1) Read every top‑level menuentry title (handles leading whitespace & both quote styles)
mapfile -t ENTRIES < <(
grep -E "^[[:space:]]*menuentry [\"'].*[\"']" "$CFG" \
| sed -E "s/^[[:space:]]*menuentry [\"']([^\"']+)[\"'].*/\1/"
)
if [ ${#ENTRIES[@]} -eq 0 ]; then
echo "No GRUB entries found in $CFG" >&2
exit 1
fi
# 2) Print numbered menu
echo "Select which OS (or kernel) to boot *next* time only:"
for i in "${!ENTRIES[@]}"; do
printf " %2d) %s\n" "$i" "${ENTRIES[$i]}"
done
echo
# 3) Prompt
read -rp "Enter choice number: " CHOICE
# 4) Validate
if ! [[ "$CHOICE" =~ ^[0-9]+$ ]] || (( CHOICE < 0 || CHOICE >= ${#ENTRIES[@]} )); then
echo "Invalid selection." >&2
exit 1
fi
# 5) Reboot into the selected title
TITLE="${ENTRIES[$CHOICE]}"
echo "Will reboot into \"$TITLE\" on next boot…"
sudo grub-reboot "$TITLE"
sudo reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment