XMLStarlet ed -s 在 1.3.1 上给出使用错误,在 1.6.1 上工作——有什么变化?

XMLStarlet ed -s gives usage error on 1.3.1, works on 1.6.1 -- what changed?

我正在使用 xmlstarlet 编辑几个 xml 配置文件。默认文件如下所示:

<configuration>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
</configuration>

我正在添加几个 <property> 子节点。像这样:

    sudo xmlstarlet ed -L \
      -s '//configuration' -t elem -n "property" \
      -s '//configuration/property[last()]' -t elem -n "name" -v "test-1" \
      -s '//configuration/property[last()]' -t elem -n "value" -v "00001" \
      -s '//configuration' -t elem -n "property" \
      -s '//configuration/property[last()]' -t elem -n "name" -v "test-2" \
      -s '//configuration/property[last()]' -t elem -n "value" -v "00002" \
      /etc/path/to/file.xml

我在本地使用 XMLStarlet 1.6.1 对此进行了测试,它运行良好,完全符合我的要求。结果如下所示:

<configuration>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name>test-1</name>
    <value>00001</value>
  </property>
  <property>
    <name>test-2</name>
    <value>00002</value>
  </property>
</configuration>

然而,当我将其放入 shell 脚本中,然后在我的远程实例(使用 XMLStarlet 1.3.1)上尝试 运行 时,它会不断打印出包的帮助文本,例如当您在终端中输入 xmlstarlet 时。我不知道是什么原因造成的

[remote-machine ~]$ xmlstarlet
XMLStarlet Toolkit: Command line utilities for XML
Usage: xmlstarlet [<options>] <command> [<cmd-options>]
where <command> is one of:
  ed    (or edit)      - Edit/Update XML document(s)
  sel   (or select)    - Select data or query XML document(s) (XPATH, etc)
  tr    (or transform) - Transform XML document(s) using XSLT
  val   (or validate)  - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)
  fo    (or format)    - Format XML document(s)
  el    (or elements)  - Display element structure of XML document
  c14n  (or canonic)   - XML canonicalization
  ls    (or list)      - List directory as XML
  esc   (or escape)    - Escape special XML characters
  unesc (or unescape)  - Unescape special XML characters
  pyx   (or xmln)      - Convert XML into PYX format (based on ESIS - ISO 8879)
  p2x   (or depyx)     - Convert PYX into XML
<options> are:
  --version            - show version
  --help               - show help
Wherever file name mentioned in command help it is assumed
that URL can be used instead as well.

Type: xmlstarlet <command> --help <ENTER> for command help

XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)

xmlstarlet ed -s 在这两个版本中都有效;发生了什么变化,使我的脚本版本特定?

在 XMLStarlet 1.3.1(您的远程系统上有)中,xmlstarlet ed -s-v 参数是强制性的。

因此,在添加property个元素时,需要添加-v ''

sudo xmlstarlet ed -L \
  -s '//configuration' -t elem -n "property" -v '' \
  -s '//configuration/property[last()]' -t elem -n "name" -v "test-1" \
  -s '//configuration/property[last()]' -t elem -n "value" -v "00001" \
  -s '//configuration' -t elem -n "property" -v '' \
  -s '//configuration/property[last()]' -t elem -n "name" -v "test-2" \
  -s '//configuration/property[last()]' -t elem -n "value" -v "00002" \
  /etc/path/to/file.xml