简体   繁体   English

bash 脚本写入 xml 文件的特定部分

[英]bash script to write in specific part of xml file

My problem is that this code below puts the result from the beginning of the file but I want to put it in a specific place.我的问题是下面的代码将结果放在文件的开头,但我想把它放在一个特定的位置。

#!/bin/bash -x

USER_ID=( User1 User2 User3 )
USER_CONF=/opt/test/config.xml

for i in "${USER_ID[@]}"; do
      
     printf '<user><id>ID</id><name><%s/name></user>\n' "$i" >> "$USER_CONF"
 
done

What I get now in config.xml is:我现在在 config.xml 中得到的是:

<company="external">
    <enabled>true</enabled>
    <users="allowed">
        USER_TO_INSERT_HERE
    </users>
</company>


<user><id>ID</id><name><User1/name></user>
<user><id>ID</id><name><User2/name></user>
<user><id>ID</id><name><User3/name></user>

What I want to get after the script execution in config.xml is:在 config.xml 中执行脚本后我想得到的是:

<company="external">
    <enabled>true</enabled>
    <users="allowed">
    <user><id>ID</id><name><User1/name></user>
    <user><id>ID</id><name><User2/name></user>
    <user><id>ID</id><name><User3/name></user>
    </users>
</company>

Do you know how can I record the values from the for and write them in a variable then to just sed that var in the code?您知道如何记录 for 中的值并将它们写入变量然后仅写入代码中的 sed 变量吗? I know how to sed it but don't know how to record in the var the values or something like that?我知道如何 sed 它但不知道如何在 var 中记录值或类似的东西?

First of all, <users="allowed"> in an invalid XML-node.首先, <users="allowed">在无效的 XML 节点中。 This should probably be something like <users permission="allowed"> .这应该类似于<users permission="allowed">

Please use an XML parser like to edit your 'config.xml'.请使用像这样的 XML 解析器来编辑您的“config.xml”。

$ xidel -s config.xml --xquery '
  serialize(
    x:replace-nodes(
      /,
      //users,
      <users permission="allowed">{
        for $x in ("User1","User2","User3") return
        <user><id>ID</id><name>{$x}</name></user>
      }</users>
    ),
    {"indent":true()}
  )
'
<company="external">
  <enabled>true</enabled>
  <users permission="allowed">
    <user>
      <id>ID</id>
      <name>User1</name>
    </user>
    <user>
      <id>ID</id>
      <name>User2</name>
    </user>
    <user>
      <id>ID</id>
      <name>User3</name>
    </user>
  </users>
</company="external">

Playground (here I used transform() , because x:replace-nodes() isn't yet supported here). Playground (这里我使用了transform() ,因为这里还不支持x:replace-nodes() )。

Lots of great answers over at unix.stackexchange.com .unix.stackexchange.com上有很多很好的答案。

The canonical answer for this sort of case (NOTE: not for XML in general in all cases, but for a file where there's a TOKEN on a line on it's own to be replaced - which is exactly the case you have given) is - a) Output the part of the file "up to" the line before the line with the token b) Output the replacement c) Output the rest of the file "from" the line after the line with the token这种情况的规范答案(注意:在所有情况下通常不适用于 XML,但对于在一行上有一个 TOKEN 的文件要替换 - 这正是你给出的情况)是 - 一个) Output the part of the file "up to" the line before the line with the token b) Output the replacement c) Output the rest of the file "from" the line after the line with the token

eg here's the simple sed variant (which is no where near as elegant as the r option to sed) -例如,这里是简单的 sed 变体(它远不如 sed 的r选项那么优雅) -

sed -e '/USER_TO_INSERT_HERE/,$ d' source.xml
cat replacement.xml
sed -e '1,/USER_TO_INSERT_HERE/ d' source.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM