我需要读取并修改pcmanfm.conf
当前目录中的单个文件( )。
我试过
$ augtool -At "Toml.lns incl $(pwd)/pcmanfm.conf" -I lenses/ print /files
/files
但它不起作用。toml.aug
(上/usr/share/augeas/lenses/dist/toml.aug
)开始于
(*
Module: Toml
Parses TOML files
Author: Raphael Pinson <[email protected]>
...
所以我相信我正确地输入了镜头名称(Toml.lns
)。
如果我解析不同类型的文件,相同的设置效果很好,例如
$ augtool -At "Shellvars.lns incl /tmp/vars.sh" -I lenses/ print /files
/files
/files/tmp
/files/tmp/vars.sh
/files/tmp/vars.sh/TESTINT = "2"
/files/tmp/vars.sh/TESTSTR = "\"FF\""
我已经发布了同样的问题https://github.com/hercules-team/augeas/issues/699以防这是 Augeas 中的一个错误。
我尝试解析的文件具有以下内容:
[config]
bm_open_method=0
[volume]
mount_on_startup=1
mount_removable=1
autorun=1
[ui]
always_show_tabs=0
max_tab_chars=32
win_width=1916
win_height=1149
splitter_pos=150
media_in_new_tab=0
desktop_folder_new_win=0
change_tab_on_drop=1
close_on_unmount=1
focus_previous=0
side_pane_mode=places
view_mode=compact
show_hidden=0
sort=name;ascending;
toolbar=newtab;navigation;home;
show_statusbar=1
pathbar_mode_buttons=0
我想在该[ui]
部分中添加/替换一个值。
答案1
TOML 规范指定字符串必须用引号引起来(请参阅https://toml.io/en/v1.0.0#string)。文件的更正变体如下所示
[config]
bm_open_method=0
[volume]
mount_on_startup=1
mount_removable=1
autorun=1
[ui]
always_show_tabs=0
max_tab_chars=32
win_width=1916
win_height=1149
splitter_pos=150
media_in_new_tab=0
desktop_folder_new_win=0
change_tab_on_drop=1
close_on_unmount=1
focus_previous=0
side_pane_mode="places"
view_mode="compact"
show_hidden=0
sort="name;ascending;"
toolbar="newtab;navigation;home;"
show_statusbar=1
pathbar_mode_buttons=0
然后您想要替换该ui
部分中的值。假设您想view_mode
从更改compact
为forward leaning
,并且该值位于名为 的 shell 变量中vmode
。
然后,使用tomlq
来自https://kislyuk.github.io/yq/,
$ vmode='forward leaning'
$ tomlq -t --arg vmode "$vmode" '.ui.view_mode |= $vmode' pcmanfm.conf
[config]
bm_open_method = 0
[volume]
mount_on_startup = 1
mount_removable = 1
autorun = 1
[ui]
always_show_tabs = 0
max_tab_chars = 32
win_width = 1916
win_height = 1149
splitter_pos = 150
media_in_new_tab = 0
desktop_folder_new_win = 0
change_tab_on_drop = 1
close_on_unmount = 1
focus_previous = 0
side_pane_mode = "places"
view_mode = "forward leaning"
show_hidden = 0
sort = "name;ascending;"
toolbar = "newtab;navigation;home;"
show_statusbar = 1
pathbar_mode_buttons = 0
该tomlq
工具使用jq
语法来访问和修改文档。该.ui.view_mode
路径是我们决定要更改的路径,也是$vmode
我们将其更新为的值。这是一个内部变量,我们使用 来将其设置为与命令行上同名的 shell 变量相同的值--arg
。
作为参考,该tomlq
实用程序在内部使用从 TOML 文档创建的 JSON 文档,如下所示
{
"config": {
"bm_open_method": 0
},
"volume": {
"mount_on_startup": 1,
"mount_removable": 1,
"autorun": 1
},
"ui": {
"always_show_tabs": 0,
"max_tab_chars": 32,
"win_width": 1916,
"win_height": 1149,
"splitter_pos": 150,
"media_in_new_tab": 0,
"desktop_folder_new_win": 0,
"change_tab_on_drop": 1,
"close_on_unmount": 1,
"focus_previous": 0,
"side_pane_mode": "places",
"view_mode": "compact",
"show_hidden": 0,
"sort": "name;ascending;",
"toolbar": "newtab;navigation;home;",
"show_statusbar": 1,
"pathbar_mode_buttons": 0
}
}
答案2
当 Augeas 无法解析文件时,您可以使用augcheck
脚本找出原因:
./augcheck /tmp/test.toml Toml
/tmp/augcheck.h99O5K/parse_file.aug:3.0-.54:exception thrown in test
/tmp/augcheck.h99O5K/parse_file.aug:3.5-.50:exception: Syntax error
Lens: /usr/share/augeas/lenses/dist/toml.aug:145.10-.45:
Error encountered at 20:15 (290 characters into string)
<s_previous=0\nside_pane_mode=|=|places\nview_mode=compact\nsho>
Tree generated so far:
Syntax error in lens definition
Failed to load /tmp/augcheck.h99O5K/parse_file.aug
这表明 Augeas 无法解析该行:side_pane_mode=planes
。从镜头来看,目前TOML镜头似乎只支持引用字符串。