全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 524|回复: 2
打印 上一主题 下一主题

[经验] 模拟显示 grub 启动菜单

[复制链接]
跳转到指定楼层
1#
发表于 2022-5-2 22:38:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 zxxx 于 2022-5-2 22:38 编辑



来源:https://askubuntu.com/questions/1019213/display-grub-menu-and-options-without-rebooting

grub-menu.sh - bash script

  1. #!/bin/bash

  2. # NAME: grub-menu.sh
  3. # PATH: $HOME/bin
  4. # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
  5. # DATE: Apr 5, 2018. Modified: May 7, 2018.

  6. # $TERM variable may be missing when called via desktop shortcut
  7. CurrentTERM=$(env | grep TERM)
  8. if [[ $CurrentTERM == "" ]] ; then
  9.     notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
  10.     exit 1
  11. fi

  12. AllMenusArr=()      # All menu options.
  13. # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
  14. HideUpstartRecovery=false
  15. if [[ $1 == short ]] ; then
  16.     HideUpstartRecovery=true    # override default with first passed parameter "short"
  17. elif [[ $1 == long ]] ; then
  18.     HideUpstartRecovery=false   # override default with first passed parameter "long"
  19. fi
  20. SkippedMenuEntry=false  # Don't change this value, automatically maintained
  21. InSubMenu=false     # Within a line beginning with `submenu`?
  22. InMenuEntry=false   # Within a line beginning with `menuentry` and ending in `{`?
  23. NextMenuEntryNo=0   # Next grub internal menu entry number to assign
  24. # Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
  25. ThisSubMenuMajorNo=0
  26. NextSubMenuMinorNo=0
  27. CurrTag=""          # Current grub internal menu number, zero based
  28. CurrText=""         # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
  29. SubMenuList=""      # Only supports 10 submenus! Numbered 0 to 9. Future use.

  30. while read -r line; do
  31.     # Example: "           }"
  32.     BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
  33.     if [[ $BlackLine == "}" ]] ; then
  34.         # Add menu option in buffer
  35.         if [[ $SkippedMenuEntry == true ]] ; then
  36.             NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
  37.             SkippedMenuEntry=false
  38.             continue
  39.         fi
  40.         if [[ $InMenuEntry == true ]] ; then
  41.             InMenuEntry=false
  42.             if [[ $InSubMenu == true ]] ; then
  43.                 NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
  44.             else
  45.                 NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
  46.             fi
  47.         elif [[ $InSubMenu == true ]] ; then
  48.             InSubMenu=false
  49.             NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
  50.         else
  51.             continue # Future error message?
  52.         fi
  53.         # Set maximum CurrText size to 68 characters.
  54.         CurrText="${CurrText:0:67}"
  55.         AllMenusArr+=($CurrTag "$CurrText")
  56.     fi

  57.     # Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
  58.     #          "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
  59.     if [[ $line == submenu* ]] ; then
  60.         # line starts with `submenu`
  61.         InSubMenu=true
  62.         ThisSubMenuMajorNo=$NextMenuEntryNo
  63.         NextSubMenuMinorNo=0
  64.         SubMenuList=$SubMenuList$ThisSubMenuMajorNo
  65.         CurrTag=$NextMenuEntryNo
  66.         CurrText="${line#*\'}"
  67.         CurrText="${CurrText%%\'*}"
  68.         AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

  69.     elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
  70.         # line starts with `menuentry` and ends with `{`
  71.         if [[ $HideUpstartRecovery == true ]] ; then
  72.             if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
  73.                 SkippedMenuEntry=true
  74.                 continue
  75.             fi
  76.         fi
  77.         InMenuEntry=true
  78.         if [[ $InSubMenu == true ]] ; then
  79.             : # In a submenu, increment minor instead of major which is "sticky" now.
  80.             CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
  81.         else
  82.             CurrTag=$NextMenuEntryNo
  83.         fi
  84.         CurrText="${line#*\'}"
  85.         CurrText="${CurrText%%\'*}"

  86.     else
  87.         continue    # Other stuff - Ignore it.
  88.     fi

  89. done < /boot/grub/grub.cfg

  90. LongVersion=$(grub-install --version)
  91. ShortVersion=$(echo "${LongVersion:20}")
  92. DefaultItem=0

  93. if [[ $HideUpstartRecovery == true ]] ; then
  94.     MenuText="Menu No.     ----------- Menu Name -----------"
  95. else
  96.     MenuText="Menu No. --------------- Menu Name ---------------"
  97. fi

  98. while true ; do

  99.     Choice=$(whiptail \
  100.         --title "Use arrow, page, home & end keys. Tab toggle option" \
  101.         --backtitle "Grub Version: $ShortVersion" \
  102.         --ok-button "Display Grub Boot" \
  103.         --cancel-button "Exit" \
  104.         --default-item "$DefaultItem" \
  105.         --menu "$MenuText" 24 76 16 \
  106.         "${AllMenusArr[@]}" \
  107.         2>&1 >/dev/tty)

  108.     clear
  109.     if [[ $Choice == "" ]]; then break ; fi
  110.     DefaultItem=$Choice

  111.     for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
  112.         if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
  113.             i=$i+1
  114.             MenuEntry="menuentry '"${AllMenusArr[i]}"'"
  115.             break
  116.         fi
  117.     done

  118.     TheGameIsAfoot=false
  119.     while read -r line ; do
  120.         if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
  121.         if [[ $TheGameIsAfoot == true ]]; then
  122.             echo $line
  123.             if [[ $line = *"}"* ]]; then break ; fi
  124.         fi
  125.     done < /boot/grub/grub.cfg

  126.     read -p "Press <Enter> to continue"

  127. done

  128. exit 0
复制代码



2#
 楼主| 发表于 2022-5-2 22:38:50 | 只看该作者
grub-display.sh - bash script - Ubuntu Desktop

  1. #!/bin/bash

  2. # NAME: grub-display.sh
  3. # PATH: $HOME/bin
  4. # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
  5. # DATE: Mar 24, 2018. Modified: Mar 26, 2018.

  6. # $TERM variable may be missing when called via desktop shortcut
  7. CurrentTERM=$(env | grep TERM)
  8. if [[ $CurrentTERM == "" ]] ; then
  9.     notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
  10.     exit 1
  11. fi

  12. # Must have the dialog package. On Servers, not installed by default
  13. command -v dialog >/dev/null 2>&1 || { echo >&2 "dialog package required but it is not installed.  Aborting."; exit 99; }

  14. # Version without upstart and recovery options displayed
  15. #awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
  16. #        | grep -v upstart | grep -v recovery > ~/.grub-display-menu

  17. # Version with upstart and recovery options displayed
  18. awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
  19.         > ~/.grub-display-menu

  20. MenuArr=()

  21. while read -r line; do
  22.     MenuNmbr=${line%% *}
  23.     MenuName=${line#* }
  24.     MenuArr+=($MenuNmbr "$MenuName")
  25. done < ~/.grub-display-menu
  26. rm ~/.grub-display-menu

  27. LongVersion=$(grub-install --version)
  28. ShortVersion=$(echo "${LongVersion:20}")
  29. DefaultItem=0

  30. while true ; do

  31.     Choice=$(dialog \
  32.         --title "Use arrow, page, home & end keys. Tab toggle option" \
  33.         --backtitle "Grub Version: $ShortVersion" \
  34.         --ok-label "Display Grub Boot" \
  35.         --cancel-label "Exit" \
  36.         --default-item "$DefaultItem" \
  37.         --menu "Menu Number       ----------- Menu Name ----------" 24 76 16 \
  38.         "${MenuArr[@]}" \
  39.         >/dev/tty)

  40.     clear
  41.     if [[ $Choice == "" ]]; then break ; fi
  42.     DefaultItem=$Choice

  43.     for (( i=0; i < ${#MenuArr[@]}; i=i+2 )) ; do
  44.         if [[ "${MenuArr[i]}" == $Choice ]] ; then
  45.             i=$i+1
  46.             MenuEntry="menuentry '"${MenuArr[i]}"'"
  47.             break
  48.         fi
  49.     done

  50.     TheGameIsAfoot=false
  51.     while read -r line ; do
  52.         if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
  53.         if [[ $TheGameIsAfoot == true ]]; then
  54.             echo $line
  55.             if [[ $line = *"}"* ]]; then break ; fi
  56.         fi
  57.     done < /boot/grub/grub.cfg

  58.     read -p "Press <Enter> to continue"

  59. done

  60. exit 0
复制代码



grub-display-lite.sh - bash script - Ubuntu Server

  1. #!/bin/bash

  2. # NAME: grub-display-lite.sh
  3. # PATH: $HOME/bin
  4. # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
  5. # DATE: Mar 26, 2018.
  6. # NOTE: "lite" version written for Ubuntu Server and Lubuntu which do
  7. #       not have `dialog` installed by default. `whiptail` is used
  8. #       instead. Nice consequences are better resolution, mouse scroll
  9. #       wheel and copy to clipboard support.

  10. # $TERM variable may be missing when called via desktop shortcut
  11. CurrentTERM=$(env | grep TERM)
  12. if [[ $CurrentTERM == "" ]] ; then
  13.     notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
  14.     exit 1
  15. fi

  16. # Version without upstart and recovery options displayed
  17. awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
  18.         | grep -v upstart | grep -v recovery > ~/.grub-display-menu

  19. # Version with upstart and recovery options displayed
  20. #awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
  21. #        > ~/.grub-display-menu

  22. MenuArr=()

  23. while read -r line; do
  24.     MenuNmbr=${line%% *}
  25.     MenuName=${line#* }
  26.     MenuArr+=($MenuNmbr "$MenuName")
  27. done < ~/.grub-display-menu
  28. rm ~/.grub-display-menu

  29. LongVersion=$(grub-install --version)
  30. ShortVersion=$(echo "${LongVersion:20}")
  31. DefaultItem=0

  32. while true ; do

  33.     Choice=$(whiptail \
  34.         --title "Use arrow, page, home & end keys. Tab toggle option" \
  35.         --backtitle "Grub Version: $ShortVersion" \
  36.         --ok-button "Display Grub Boot" \
  37.         --cancel-button "Exit" \
  38.         --default-item "$DefaultItem" \
  39.         --menu "Menu Number       ----------- Menu Name ----------" 24 76 16 \
  40.         "${MenuArr[@]}" \
  41.        >/dev/tty)

  42.     clear
  43.     if [[ $Choice == "" ]]; then break ; fi
  44.     DefaultItem=$Choice

  45.     for (( i=0; i < ${#MenuArr[@]}; i=i+2 )) ; do
  46.         if [[ "${MenuArr[i]}" == $Choice ]] ; then
  47.             i=$i+1
  48.             MenuEntry="menuentry '"${MenuArr[i]}"'"
  49.             break
  50.         fi
  51.     done

  52.     TheGameIsAfoot=false
  53.     while read -r line ; do
  54.         if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
  55.         if [[ $TheGameIsAfoot == true ]]; then
  56.             echo $line
  57.             if [[ $line = *"}"* ]]; then break ; fi
  58.         fi
  59.     done < /boot/grub/grub.cfg

  60.     read -p "Press <Enter> to continue"

  61. done

  62. exit 0
复制代码

3#
发表于 2022-5-2 22:46:34 | 只看该作者
TUI 玩出花来了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-12-16 07:52 , Processed in 0.062669 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表