Sav*_*vic 6 python parsing grub
有谁知道 grub2 的 grub.cfg 文件的 python 解析器?
我正在尝试通过设备分区(根)获取“菜单项”,例如
hd0,msdos1: ['Ubuntu, with Linux 3.0.0-15-generic',
'Ubuntu, with Linux 3.0.0-15-generic (recovery mode)',
'Ubuntu, with Linux 3.0.0-14-generic']
hd2,msdos1: ["Windows 7 (on /dev/sdc1)"]
Run Code Online (Sandbox Code Playgroud)
等等。
解决方案:
re.findall("menuentry ['\"](.*?)['\"].*?set root='(.*?)'", x, re.S)
Run Code Online (Sandbox Code Playgroud)
[('Ubuntu, with Linux 3.0.0-15-generic', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-15-generic (recovery mode)', '(hd0,msdos1) '), ('Ubuntu, with Linux 3.0.0-14-generic', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-14-generic (recovery mode)', '(hd0, msdos1)'), ('Ubuntu, with Linux 3.0.0-13-generic', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-13-generic (recovery mode)', '( hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-12-generic', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-12-generic (recovery mode)', '(hd0,msdos1)'), ('内存测试 (memtest86+)', '(hd0,msdos1)'), ('内存测试 (memtest86+, 串口控制台 115200)', '(hd0,msdos1)'), ( 'Windows 7(在 /dev/sdc1 上)', '(hd2,msdos1)')]
我不知道 grub.cfg 的 Python 解析器,但您不需要解析整个文件来获取该信息。这是您要查找的数据的格式:
menuentry "<name>" [options] {
...
set root='<root>'
...
}
Run Code Online (Sandbox Code Playgroud)
因此,查找以 开头的行menuentry,解析该行的名称,然后使用}for扫描直到下一行set root=。