Skip to content

Instantly share code, notes, and snippets.

@hansfn
Last active April 19, 2023 13:24
Show Gist options
  • Save hansfn/c15e700eff2994c8f14b52492f210321 to your computer and use it in GitHub Desktop.
Save hansfn/c15e700eff2994c8f14b52492f210321 to your computer and use it in GitHub Desktop.
A simple (g)awk script that lists the grub menu from the command line. Typically used when you use grub-set-default or grub-reboot. Tested with Grub 2 on Ubuntu 22.04. Assumes grub.cfg is well formed.
gawk 'BEGIN {
in_submenu=0
menuindex=0
}
# Skip lines not menu related
!/.*menu.*{.*/ { next; }
# Do the work for each menu line
{
match( $0, /^[^'\'']*'\''([^'\'']*)'\''.*$/, menu_arr )
if ( $0 ~ /^submenu.*{.*/ ) {
submenu=0
in_submenu=1
}
if ( $0 ~ /^menuentry.*{.*/ ) {
submenu=0
in_submenu=0
}
if (submenu == 0 ) {
print menuindex ": " menu_arr[1]
menuindex++
} else {
print " " (menuindex - 1) ">" (submenu - 1) " " menu_arr[1]
}
if (in_submenu == 1 ) {
submenu++
}
}' /boot/grub/grub.cfg
@hansfn
Copy link
Author

hansfn commented Apr 19, 2023

It seems my work was wasted. The following one-liner does the same job:

awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg

PS! All of this is coming from https://askubuntu.com/q/599208/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment