-
Notifications
You must be signed in to change notification settings - Fork 4
/
select-default-grub-entry.sh
executable file
·73 lines (53 loc) · 1.65 KB
/
select-default-grub-entry.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
# Author: Eric Zhiqiang Ma (http://www.ericzma.com)
# How to use this script:
# http://www.systutorials.com/3826/setting-default-entry-in-grub2-and-grub/
# TODO:
# fix bug in checking "Invalid selection"
if [ -d /sys/firmware/efi ]; then
grubcfg="/etc/grub2-efi.cfg"
else
grubcfg="/etc/grub2.cfg"
fi
grep "^menuentry" $grubcfg | cut -d "'" -f2 >/tmp/grub2-select.entries
items=`cat /tmp/grub2-select.entries`
linen=`cat /tmp/grub2-select.entries | wc -l`
j=0
while [ $j -lt $linen ]
do
let "j=j+1"
echo -n "$j "
echo "$items" | head -n $j | tail -n1
done
# olddef=`grub2-editenv list`
#
# echo "Old default:"
# echo $olddef
echo -n "Your select: "
read sel
if [ $sel -lt "0" ] && [ $sel -ge $linen ]; then
echo "Invalid selection"
exit 0
fi
# echo "You select $sel"
selected=`echo "$items" | head -n $sel | tail -n1`
echo "Entry selected: $selected"
# set-default and change default entry files
# to make double insurance. Seems not every versions of grub2 respect
# both.
grub2-set-default "$selected"
# change the default entry in /etc/default/grub
# save a copy first
cp /etc/default/grub /etc/default/grub-grub2-select.bak
# change the default entry now
sed -i "s/GRUB_DEFAULT=.*/GRUB_DEFAULT=\"$selected\"/g" /etc/default/grub
# make a backup just in case
cp $grubcfg $grubcfg-grub2-select.bak
# regenerate the config file now
# grub2-mkconfig of some versions do not respect the symbolic links
# so generate to a tmporary file and then use cp to update the grubcfg file
tmpfile=/tmp/$(basename $0)-grub2.cfg
grub2-mkconfig -o $tmpfile && cp $tmpfile $grubcfg
# newdef=`grub2-editenv list`
# echo "New default:"
# echo $newdef