2

I use GRUB v2 as my boot manager.
Whenever I want to reboot to another operating system (or menu entry, like MemTest), I use the command:

# grub-reboot n

where n is the number (starting on 0) of the desired menu entry.
For example:

GRUB2 menu example

so, if, in the above example, I want to reboot to Squeeze Linux I will enter:

# grub-reboot 4

But, when having multiple computers and/or operating systems, it is hard to remember the number of this entry, and I finally need to reboot to the menu to check for the number.

I know the entries are stored at /boot/grub/grub.cfg on lines named menuentry or "submenu` but, as long as GRUB becomes more complex (and advanced, of course), this file is a bit awkward to read in order to count number of entries.

Is there a practical way to know the number of all the entries of the GRUB v2 boot menu?

1 Answer 1

2

I use this command line, that is just a parsing of /boot/grub/grub.cfg, in order to show numbered entries, one per line:

sed ':again;$!N;$!b again; :b; s/{[^{}]*}//g; t b' /boot/grub/grub.cfg | cut -c -60 | grep -e "^menuentry" -e "submenu" | nl -v 0 | grep -e "menuentry" -e "submenu" --color

that results (not the same example as the graphic before, sorry):

GRUB-ListMenu results example

My preferred method is to add it as an alias or function. For example, as a function in ~/bashrc :

# List entries numbers for grub-reboot:
function GRUB-ListMenu() {
        sed ':again;$!N;$!b again; :b; s/{[^{}]*}//g; t b' /boot/grub/grub.cfg | cut -c -60 | grep -e "^menuentry" -e "submenu" | nl -v 0 | grep -e "menuentry" -e "submenu" --color
}

Explanation:

  • The sed command removes all lines between curly brackets ({ and }), due they correspond to drop-down submenus.
  • The cut command leaves only the first 60 characters, in order to make results more readable for the user.
  • The grep command leaves only the lines corresponding to real menu entries: menuentry and submenu.
  • The nl command numerates each line (starting from 0, of course).
  • There is an addition grep command, not strictly needed, that simply adds colors to the results ;-) .

Advices about optimizations are welcome.
Tested working on GRUB v2.02~beta2-9ubuntu1 running on Ubuntu Desktop, Server, v12, v14, Kali and BackTrack. Feedbacks about behavior in another Linux are encouraged (I will add them to this post).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .