Ubuntu 22.04 / Windows 10 双系统 GRUB 切换默认启动项的脚本

兴趣和需要使然,几乎每台电脑都安装了 Ubuntu 和 Windows 10 双系统,我用的过程是先安装 Windows 10,再 Ubuntu 22.04,使用 Ubuntu 的 GRUB 做启动管理。

默认启动项信息在 Ubuntu 系统的 /boot/grub/grub.cfg 中设置的,但编辑该文件需要 root 权限,翻来翻去比较繁琐,所以编写了段 python3 代码来做这个事。

Python3 文件 GrubDefaultBoot.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 26 11:57:54 2022

@author: farman
"""


def read_menu_entry_level1(file):
    lines = open(file).readlines()
    entries = []
    valid   = []
    
    
    for line  in lines:
        line = line.rstrip()
        
        if not line.count("menuentry") and not line.count("submenu"):
            continue
        
        if line.find("menuentry") and line.find("submenu"): # menuentry or submenu appears at the beginning
            continue
        
        #print(line)
        
        begin = line.find("'") + 1
        end   = line.find("'", begin)
        
        valid.append(not line.find("menuentry"))
        
        line = line[begin:end]
        entries.append(line)
        
    return entries, valid
        

def read_menu_entry_default(file):
    lines = open(file).readlines()
    
    for line in lines:
        if line.count("set default=\"") and not line.count("set default=\"$"):
            line  = line.strip()
            begin = line.find("\"") + 1
            end   = line.find("\"", begin)
            default_entry_index = int(line[begin:end])
    
    return default_entry_index


def select(entries, valid, default_entry_index):
    count = 0
    print("\n\nBoot entries")
    print("-" * 40)
    
    for e in entries:
        print("%5d - %s"%(count, e))
        count += 1
    
    print("-" * 40)
    ans = input("Entry to set as default : ")
    
    ans = ans.strip()
    
    if not len(ans):
        ans = select(entries, valid, default_entry_index)
    
    if not ans.isdecimal():
        ans = select(entries, valid, default_entry_index)
        
    ans = int(ans)
    
    if ans < 0  or ans >= len(entries):
        ans = select(entries, valid, default_entry_index)
        
    return ans


def save_selection(file, default_entry_index):
    lines = open(file).readlines()
    new_lines = []
    
    for line in lines:
        if line.count("set default=\"") and not line.count("set default=\"$"):
            line = line[:line.find('"')]
            line += '"%d"\n'%default_entry_index
        
        new_lines.append(line)
    
    open(file, "w").writelines(new_lines)
    return

#------------------------------------------------------------------------------

if __name__ == "__main__":
    file = "/boot/grub/grub.cfg"
    entries, valid = read_menu_entry_level1(file)
    default_entry_index = read_menu_entry_default(file)
    selected = select(entries, valid, default_entry_index)
    save_selection(file, selected)

 Bash 脚本文件 GrubDefaultBoot.sh

#!/bin/bash
sudo python3 GrubDefaultBoot.py

运行下面的指令为 GrubDefaultBoot.sh 增加可执行属性

chmod +x GrubDefaultBoot.sh

使用时候,直接运行 GrubDefaultBoot.sh 即可,即在 shell 中切换到脚本所在路径,运行

./GrubDefaultBoot.sh

输入密码后,列出 /boot/grub/grub.cfg 可用的一级启动项目,我的如下

Boot entries
----------------------------------------
    0 - Ubuntu
    1 - Advanced options for Ubuntu
    2 - Windows Boot Manager (on /dev/sda2)
    3 - UEFI Firmware Settings
----------------------------------------
Entry to set as default : 

输入想要的启动项之前的数字回车,就会将选定的启动项设置为默认启动项目。

接下来就可以重启计算机试试看。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值