diff --git a/reboot-to-os b/reboot-to-os new file mode 100755 index 0000000..049efcf --- /dev/null +++ b/reboot-to-os @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +declare -a entries +mapfile -t entries < <(awk 'BEGIN { + l=0 + menuindex= 0 + stack[t=0] = 0 +} + +function push(x) { + stack[t++] = x +} + +function pop() { + if ( t > 0 ) { + return stack[--t] + } else { + return "" + } +} + +{ + +if ( $0 ~ /.*menu.*{.*/ ) { + push( $0 ) + l++ +} else if ( $0 ~ /.*{.*/ ) { + push( $0 ) +} else if ( $0 ~ /.*}.*/ ) { + X = pop() + if( X ~ /.*menu.*{.*/ ) { + l-- + match( X, /^[^'\'']*'\''([^'\'']*)'\''.*$/, arr ) + + if ( l == 0 ) { + print menuindex ": " arr[1] + menuindex++ + submenu=0 + } else { + print " " (menuindex-1) ">" submenu ": " arr[1] + submenu++ + } + } +} + +}' /boot/grub/grub.cfg) + +declare selected_entry +selected_entry="$(printf "%s\n" "${entries[@]}" | fzf --cycle --tiebreak=begin,chunk,length --layout=reverse | xargs)" + +sudo grub-reboot "$(echo "${selected_entry}" | cut -d':' -f1)" + +read -p "Reboot Now? [yN] " -n 1 -r +echo +case $REPLY in + y|Y) + echo + echo "[countdown] rebooting in..." + + secs=$((5)) + while [ $secs -ge 0 ]; do + sleep 1 & + printf "\r %d " $(( secs )) + secs=$(( secs - 1 )) + wait + done + echo + sudo reboot + ;; + + *) + echo "Next reboot would boot to:" + echo " ${selected_entry}" + ;; +esac