我想更新XML配置文件中的一个部分,或者如果已经使用Augeas不存在则添加一个新部分。
XML文件如下所示:
<?xml version="1.0"?>
<security>
<users>
...
<user>
<id>deployment</id>
<firstName>Deployment</firstName>
<lastName>User</lastName>
<password>somepasshere</password>
<status>active</status>
<email>changeme1@yourcompany.com</email>
</user>
</users>
</security>
如果ID已经存在,我想更新姓氏/名字/电子邮件;如果是新ID,我想添加新用户部分。
在AugTool中我使用:
augtool> set /augeas/load/Xml/lens Xml.lns
augtool> set /augeas/load/Xml/incl /security.xml
augtool> load
我还在学习Augeas,所以这是我第一次尝试获取节点:
augtool> print /files/security.xml/security/users/user/*[ #text= 'deployment']
在用户中更新或创建新部分用户的命令是什么?
谢谢你!答案 0 :(得分:6)
首先,使用最近的Augeas版本(&gt; = 1.0.0),您可以使用--transform
来设置转换。假设文件为./users.xml
:
$ augtool -r . --noautoload --transform "Xml.lns incl /users.xml"
augtool> defnode user /files/users.xml/security/users/user[id/#text="deployment"] # Create a new user entry if it doesn't exist yet, assign node to the "user" variable
augtool> set $user/id/#text "deployment" # Set id if node was just created
augtool> set $user/firstName/#text "Deployment" # Set firstName
augtool> set $user/lastName/#text "User" # Set lastName
augtool> set $user/email/#text "changeme1@yourcompany.com" # set email
...
augtool> save
Saved 1 file(s)
您甚至可以将其转换为脚本,例如user.augtool
:
#!/usr/bin/augtool -sf
defnode user /files/users.xml/security/users/user[id/#text="deployment"]
set $user/id/#text "deployment"
set $user/firstName/#text "Deployment"
set $user/lastName/#text "User"
set $user/email/#text "changeme1@yourcompany.com"
然后您可以启动:
$ chmod +x user.augtool
$ ./user.augtool --transform "Xml.lns incl /users.xml" -r .
Saved 1 file(s)