Create or update a section in XML file with Augeas

好久不见. 提交于 2019-12-05 06:48:32

First, with recent Augeas versions (>= 1.0.0), you can use --transform to set up the transformation. Let's say the file is ./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)

You can even turn this into a script, say 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"

which you can then launch:

$ chmod +x user.augtool
$ ./user.augtool --transform "Xml.lns incl /users.xml" -r .
Saved 1 file(s)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!