|
Line 0
Link Here
|
|
|
1 |
#!/bin/sh -efu |
| 2 |
### This file is covered by the GNU General Public License |
| 3 |
### version 2 or later. |
| 4 |
### |
| 5 |
### Copyright (C) 2019 ALT Linux Team |
| 6 |
### Author: Leonid Krivoshein <klark@altlinux.org> |
| 7 |
|
| 8 |
numbers_only=0 |
| 9 |
titles_only=0 |
| 10 |
search_item= |
| 11 |
progname="${0##*/}" |
| 12 |
grubmenu="/boot/grub/grub.cfg" |
| 13 |
|
| 14 |
|
| 15 |
show_help() { |
| 16 |
cat <<-EOF |
| 17 |
$progname - Show default or specified grub menu. |
| 18 |
|
| 19 |
Usage: $progname [options] |
| 20 |
|
| 21 |
Options: |
| 22 |
-f, --config= Specify path to the grub config. |
| 23 |
-n, --numbers Show only menu item sequence numbers. |
| 24 |
-t, --titles Show only menu item names. |
| 25 |
-c, --check= Specify menu item to validate. |
| 26 |
-V, --version Show script version and exit. |
| 27 |
-h, --help Show this help message and exit. |
| 28 |
EOF |
| 29 |
exit 0 |
| 30 |
} |
| 31 |
|
| 32 |
show_version() { |
| 33 |
cat <<-EOF |
| 34 |
$progname version 1.0 |
| 35 |
Copyright (C) 2019, ALT Linux Team |
| 36 |
EOF |
| 37 |
exit 0 |
| 38 |
} |
| 39 |
|
| 40 |
show_usage() { |
| 41 |
[ -z "$*" ] || echo "$*" >&2 |
| 42 |
echo "Try '$progname --help' for more information." >&2 |
| 43 |
exit 1 |
| 44 |
} |
| 45 |
|
| 46 |
parse_args() { |
| 47 |
local opts="+f:,n,t,c:,V,h" |
| 48 |
local longopts="config:,numbers,titles,check:,version,help" |
| 49 |
|
| 50 |
opts=$(getopt -n "$progname" -o "$opts" -l "$longopts" -- "$@") || |
| 51 |
show_usage "Invalid command-line arguments." |
| 52 |
eval set -- "$opts" |
| 53 |
while [ $# -gt 0 ]; do |
| 54 |
case "$1" in |
| 55 |
-f|--config) shift; grubmenu="$1";; |
| 56 |
-n|--numbers) numbers_only=1; titles_only=0;; |
| 57 |
-t|--titles) numbers_only=0; titles_only=1;; |
| 58 |
-c|--check) shift; search_item="$1";; |
| 59 |
-V|--version) show_version;; |
| 60 |
-h|--help) show_help;; |
| 61 |
--) shift; break;; |
| 62 |
-*) show_usage "Unrecognized option: '$1'";; |
| 63 |
*) break;; |
| 64 |
esac |
| 65 |
shift |
| 66 |
done |
| 67 |
[ $# -eq 0 ] || show_usage "Unrecognized arguments: '$*'" |
| 68 |
} |
| 69 |
|
| 70 |
show_menu() { |
| 71 |
local mode title number prefix entry=0 itemno=0 |
| 72 |
|
| 73 |
cat "$grubmenu" | sed \ |
| 74 |
-re '/^\s*(menuentry\s+|submenu\s+|})/!d' \ |
| 75 |
-re "s/^\s*menuentry\s+'([^']+)'.*\$/M\t\1/g" \ |
| 76 |
-re 's/^\s*menuentry\s+"([^"]+)".*$/M\t\1/g' \ |
| 77 |
-re "s/^\s*submenu\s+'([^']+)'.*\$/S\t\1/g" \ |
| 78 |
-re 's/^\s*submenu\s+"([^"]+)".*$/S\t\1/g' \ |
| 79 |
-re "s/^\s*}\s*\$/E\t-/g" | |
| 80 |
while read mode title; do |
| 81 |
case "$mode" in |
| 82 |
M) if [ $numbers_only -ne 0 ]; then |
| 83 |
echo -e "${number}${itemno}" |
| 84 |
elif [ $titles_only -ne 0 ]; then |
| 85 |
echo -e "${prefix}${title}" |
| 86 |
else |
| 87 |
echo -e "${number}${itemno}\t${prefix}${title}" |
| 88 |
fi |
| 89 |
itemno=$(($itemno + 1)) |
| 90 |
entry=1 |
| 91 |
;; |
| 92 |
S) number="${number}${itemno}>" |
| 93 |
prefix="${prefix}${title}>" |
| 94 |
itemno=0 |
| 95 |
;; |
| 96 |
*) if [ $entry -ne 0 ]; then |
| 97 |
entry=0 |
| 98 |
elif [ -n "$number" ]; then |
| 99 |
number="${number%>}" |
| 100 |
prefix="${prefix%>}" |
| 101 |
case "$number" in |
| 102 |
*">"*) itemno="${number##*>}" |
| 103 |
number="${number%>*}>" |
| 104 |
prefix="${prefix%>*}>" |
| 105 |
itemno=$(($itemno + 1)) |
| 106 |
;; |
| 107 |
*) itemno=$number |
| 108 |
itemno=$(($itemno + 1)) |
| 109 |
prefix= |
| 110 |
number= |
| 111 |
;; |
| 112 |
esac |
| 113 |
fi |
| 114 |
;; |
| 115 |
esac |
| 116 |
done |
| 117 |
} |
| 118 |
|
| 119 |
check_item() { |
| 120 |
local cnt |
| 121 |
|
| 122 |
numbers_only=1; titles_only=0 |
| 123 |
cnt=$(show_menu | grep -swc "$search_item" ||:) |
| 124 |
[ "$cnt" != "1" ] || exit 0 |
| 125 |
|
| 126 |
numbers_only=0; titles_only=1 |
| 127 |
cnt=$(show_menu | grep -swc "$search_item" ||:) |
| 128 |
[ "$cnt" != "1" ] || exit 0 |
| 129 |
|
| 130 |
[ -z "$cnt" -o "$cnt" = "0" ] && |
| 131 |
echo "$search_item: menu item not found." >&2 || |
| 132 |
echo "$search_item: duplicate items found." >&2 |
| 133 |
exit 1 |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
parse_args "$@" |
| 138 |
[ -r "$grubmenu" ] || show_usage "$grubmenu: config file not found." |
| 139 |
[ -z "$search_item" ] || check_item |
| 140 |
show_menu |
| 141 |
|