需要能够使用 shell 脚本将字符串(路径)添加到 XML 文件

need to be able to add strings (paths) to an XML file using shell script

我需要能够使用 shell 脚本将字符串(路径)添加到 XML 文件, 这是我拥有的XML:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<configurations>
    <smtpHost>smtp3.gmail.com</smtpHost>
    <smtpPort>25</smtpPort>
    <emailFrom>GitPushNotifier@gmail.com</emailFrom>
    <emailSubject>Push notification</emailSubject>
    <!-- Stash general URL-->
    <gitViewerURL>http://server0005.gmail.net:7990/projects/</gitViewerURL>
    <rule>
        <name>test_12.55.4</name>
        <repo>test</repo>
        <branch>refs/heads/12.55.4</branch>
        <emailTo>test@gmail.com</emailTo>
        <path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml</path>
    </rule>
    <rule>
        <name>test_12.55.10</name>
        <repo>test</repo>
        <branch>refs/heads/12.55.10</branch>
        <emailTo>test@gmail.com</emailTo>
        <path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml</path>
    </rule>
    <rule>
        <name>test_12.55.6</name>
        <repo>test</repo>
        <branch>refs/heads/12.55.6</branch>
        <emailTo>test@gmail.com</emailTo>
        <path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml</path>
    </rule>
</configurations>

而且我只需要为一个版本添加到子节点的附加路径(比如 test_12.55.10),我要添加的路径是:

服务器/./resources/schema/v12_55_10/./.-dbSchemaDescriptor.xml, 服务器/./resources/SpringIOC/dataupgrader/v12_55_10/./.-dataUpgrader.xml, 服务器/./java/com/hp/mqm/dataupgrader/v12_55_10/./..java, 服务器/./resources/indexes/v12_55_10/.*.index

我想使用 sed 或 "xmlstarlet" 正如我看到其他人建议的那样, 所以我想要得到的输出是:

<path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml,Server/.*/resources/schema/v12_55_10/.*/.*-dbSchemaDescriptor\.xml,
Server/.*/resources/SpringIOC/dataupgrader/v12_55_10/.*/.*-dataUpgrader\.xml,
Server/.*/java/com/hp/mqm/dataupgrader/v12_55_10/.*/.*\.java,
Server/.*/resources/indexes/v12_55_10/.*\.index</path>

用sed就可以了。这是一个小的 sed 脚本,只要 pathrule:

中跟在 name 之后就可以工作

script.sed

# filter for block from name to end of rule with the version:
/<name>test_12.55.10<\/name>/,/<\/rule>/ {
    /<\/path>/ {
                # this is a multiline second argument to s:
                s+<\/path>+,\
                    Server/./resources/schema/v12_55_10/./.-dbSchemaDescriptor.xml,\
                    Server/./resources/SpringIOC/dataupgrader/v12_55_10/./.-dataUpgrader.xml,\
                    Server/./java/com/hp/mqm/dataupgrader/v12_55_10/./..java,\
                    Server/./resources/indexes/v12_55_10/.*.index\
                </path>\
                +;
                # remove the whitespaces inserted above for readability
                s/,[ \n]+Server//g;
                }
    };

你这样使用它:sed -rf script.sed yourfile.xml.

您可以从 shell 脚本中填写版本号和其他路径(注意每个 Server 行后的其他反斜杠)。

这是您可以像最初要求的那样使用 xmlstarlet 实现的一种方法...

name="test_12.55.10"
path=", Server/./resources/schema/v12_55_10/./.-dbSchemaDescriptor.xml, Server/./resources/SpringIOC/dataupgrader/v12_55_10/./.-dataUpgrader.xml, Server/./java/com/hp/mqm/dataupgrader/v12_55_10/./..java, Server/./resources/indexes/v12_55_10/.*.index"

xmlstarlet ed -L -a "/*/rule[name='$name']/path/text()" --type text -n "" -v "$path" input.xml

注意:这将修改文件。(因为 -L

如果您不使用变量,这可能是单行代码。