The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 106,270โ€…    Votes:  17โ€…
Tags: command-line   grub2  
Link: ๐Ÿ” See Original Answer on Ask Ubuntu ๐Ÿ”—

Title: How to list GRUB's "menuentries" in command-line?
ID: /2018/04/07/How-to-list-GRUB_s-_menuentries_-in-command-line_
Created: April 7, 2018    Edited:  July 27, 2019
Upload: November 24, 2022    Layout:  post
TOC: true    Navigation:  true    Copy to clipboard:  false

ToC Skip

Color Screen shot (short version)

Use mouse scroll wheel, Home, End, PgUp, PgDn, โ†‘ and โ†“ keys to navigate the menu.

Top ToS Skip

Table of Contents

Top ToS ToC Skip

Text Screen shot (long version)

The bash script uses whiptail instead of dialog to display the menu. One advantage is you can copy the terminal image to the clipboard as text and paste it into this website as text. Other advantages include:

Hereโ€™s a text screen shot:

Grub Version: 2.02~beta2-36ubuntu3.15

        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค Use arrow, page, home & end keys. Tab toggle option โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ Menu No. --------------- Menu Name ---------------                       โ”‚ 
        โ”‚                                                                          โ”‚ 
        โ”‚     0    Ubuntu                                                     โ†‘    โ”‚ 
        โ”‚     1    Advanced options for Ubuntu                                โ–ฎ    โ”‚ 
        โ”‚     1>0  Ubuntu, with Linux 4.14.31-041431-generic                  โ–’    โ”‚ 
        โ”‚     1>1  Ubuntu, with Linux 4.14.31-041431-generic (upstart)        โ–’    โ”‚ 
        โ”‚     1>2  Ubuntu, with Linux 4.14.31-041431-generic (recovery mode)  โ–’    โ”‚ 
        โ”‚     1>3  Ubuntu, with Linux 4.14.30-041430-generic                  โ–’    โ”‚ 
        โ”‚     1>4  Ubuntu, with Linux 4.14.30-041430-generic (upstart)        โ–’    โ”‚ 
        โ”‚     1>5  Ubuntu, with Linux 4.14.30-041430-generic (recovery mode)  โ–’    โ”‚ 
        โ”‚     1>6  Ubuntu, with Linux 4.14.27-041427-generic                  โ–’    โ”‚ 
        โ”‚     1>7  Ubuntu, with Linux 4.14.27-041427-generic (upstart)        โ–’    โ”‚ 
        โ”‚     1>8  Ubuntu, with Linux 4.14.27-041427-generic (recovery mode)  โ–’    โ”‚ 
        โ”‚     1>9  Ubuntu, with Linux 4.14.24-041424-generic                  โ–’    โ”‚ 
        โ”‚     1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart)        โ–’    โ”‚ 
        โ”‚     1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode)  โ–’    โ”‚ 
        โ”‚     1>12 Ubuntu, with Linux 4.14.23-041423-generic                  โ–’    โ”‚ 
        โ”‚     1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart)        โ†“    โ”‚ 
        โ”‚                                                                          โ”‚ 
        โ”‚                                                                          โ”‚ 
        โ”‚                   <Display Grub Boot>        <Exit>                      โ”‚ 
        โ”‚                                                                          โ”‚ 

Top ToS ToC Skip

Highlight entry and press Enter

Use the navigation keys to highlight an option and press Enter to see the pre-kernel drivers loaded by grub and the boot parameters passed by grub to the kernel when booting it:

menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
echo 'Loading Linux 4.14.27-041427-generic ...'
linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-4.14.27-041427-generic
Press <Enter> to continue
## ```

#`` bash script

`` only has one option to tweak:

``` bash

<a id="hdr5"></a>
<div class="hdr-bar">  <a href="#">Top</a>  <a href="#hdr4">ToS</a>  <a href="#hdr2">ToC</a>  <a href="#hdr6">Skip</a></div>

# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?

Set the value to true (hide the extra entries) or false (list all entries).

The default format can be overridden when calling the script using: short

or: long

The code:

``` bash

Top ToS ToC Skip




DESC: Written for AU Q&A:

DATE: Apr 5, 2018. Modified: July 27, 2019

UPDT: Scroll bar was outside of dialog box. Move windo border line.

$TERM variable may be missing when called via desktop shortcut

CurrentTERM=$(env | grep TERM) if [[ $CurrentTERM == โ€œโ€ ]] ; then notify-send โ€“urgency=critical โ€œ$0 cannot be run from GUI without TERM environment variable.โ€ exit 1 fi

Top ToS ToC Skip

Send output to secondary terminal such that previous history isnโ€™t cleared on exit

tput smcup

AllMenusArr=() # All menu options.

Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?

HideUpstartRecovery=false if [[ $1 == short ]] ; then HideUpstartRecovery=true # override default with first passed parameter โ€œshortโ€ elif [[ $1 == long ]] ; then HideUpstartRecovery=false # override default with first passed parameter โ€œlongโ€ fi SkippedMenuEntry=false # Donโ€™t change this value, automatically maintained InSubMenu=false # Within a line beginning with submenu? InMenuEntry=false # Within a line beginning with menuentry and ending in {? NextMenuEntryNo=0 # Next grub internal menu entry number to assign

Top ToS ToC Skip

Major / Minor internal grub submenu numbers, ie 1>0, 1>1, 1>2, etc.

ThisSubMenuMajorNo=0 NextSubMenuMinorNo=0 CurrTag=โ€โ€ # Current grub internal menu number, zero based CurrText=โ€โ€ # Current grub menu option text, ie โ€œUbuntuโ€, โ€œWindowsโ€ฆโ€, etc. SubMenuList=โ€โ€ # Only supports 10 submenus! Numbered 0 to 9. Future use.

while read -r line; do # Example: โ€œ }โ€ BlackLine=โ€${line//[[:blank:]]/}โ€ # Remove all whitespace if [[ $BlackLine == โ€œ}โ€ ]] ; then # Add menu option in buffer if [[ $SkippedMenuEntry == true ]] ; then NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 )) SkippedMenuEntry=false continue fi if [[ $InMenuEntry == true ]] ; then InMenuEntry=false if [[ $InSubMenu == true ]] ; then NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 )) else NextMenuEntryNo=$(( $NextMenuEntryNo + 1 )) fi elif [[ $InSubMenu == true ]] ; then InSubMenu=false NextMenuEntryNo=$(( $NextMenuEntryNo + 1 )) else continue # Future error message? fi # Set maximum CurrText size to 68 characters. CurrText=โ€${CurrText:0:67}โ€ AllMenusArr+=($CurrTag โ€œ$CurrTextโ€) fi

# Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
#          "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
if [[ $line == submenu* ]] ; then
    # line starts with `submenu`
    AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"
elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
    # line starts with `menuentry` and ends with `{`
    if [[ $HideUpstartRecovery == true ]] ; then
        if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
    if [[ $InSubMenu == true ]] ; then
        : # In a submenu, increment minor instead of major which is "sticky" now.
    continue    # Other stuff - Ignore it.

done < /boot/grub/grub.cfg

LongVersion=$(grub-install โ€“version) ShortVersion=$(echo โ€œ${LongVersion:20}โ€) DefaultItem=0

if [[ $HideUpstartRecovery == true ]] ; then MenuText=โ€Menu No. โ€”โ€”โ€”โ€“ Menu Name โ€”โ€”โ€”โ€“โ€ else MenuText=โ€Menu No. โ€”โ€”โ€”โ€”โ€” Menu Name โ€”โ€”โ€”โ€”โ€”โ€ fi

while true ; do

Choice=$(whiptail --clear \
    --title "Use arrow, page, home & end keys. Tab toggle option" \
    --backtitle "Grub Version: $ShortVersion" \
    --ok-button "Display Grub Boot" \
    --cancel-button "Exit" \
    --default-item "$DefaultItem" \
    --menu "$MenuText" 24 80 16 \
    "${AllMenusArr[@]}" \
    2>&1 >/dev/tty)
if [[ $Choice == "" ]]; then break ; fi

for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
    if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
        MenuEntry="menuentry '"${AllMenusArr[i]}"'"

while read -r line ; do
    if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
    if [[ $TheGameIsAfoot == true ]]; then
        echo $line
        if [[ $line = *"}"* ]]; then break ; fi
done < /boot/grub/grub.cfg

read -p "Press <Enter> to continue"


Top ToS ToC Skip

Restore output to primary terminal

tput rmcup

exit 0


Top ToS ToC
โ‡ง Why show desktop (with alt tab) in kde renders alternatively two different things? Boot-Repair created too many grub menu entries for Windows  โ‡ฉ