Xml 猛击。循环中的字符串连接

Xml 猛击。循环中的字符串连接,xml,bash,git-bash,Xml,Bash,Git Bash,我想获取sitemap.xml文件,并用新的时间戳替换lastmod sitemap.xml示例: 2020-08-02T07:30:53+00:00 1 2020-08-02T07:30:53+00:00 0.80 我的代码是: field=lastmod timestamp=“$(日期--iso-8601=秒)” sitemap=“” IFS=$'\r\n' 对于$中的行(cat./sitemap.xml) 做 案例$line in *""*""* ) pre=${line#*“”} s

我想获取
sitemap.xml
文件,并用新的时间戳替换
lastmod

sitemap.xml示例:


2020-08-02T07:30:53+00:00
1
2020-08-02T07:30:53+00:00
0.80
我的代码是:

field=lastmod
timestamp=“$(日期--iso-8601=秒)”
sitemap=“”
IFS=$'\r\n'
对于$中的行(cat./sitemap.xml)
做
案例$line in
*""*""* )
pre=${line#*“”}
suf=${line%''*}
line=“${line%$pre}${timestamp}${line#$suf}”
;;
以撒
站点地图=$sitemap$行$'\n'
完成
#echo$sitemap>sitemap.xml
echo$站点地图
上面的代码应该读取文件,替换所需的标记并输出它们。在这里,我试图连接文件的字符串,以便以后保存。我还试图在每一行中添加
\n

但此代码的输出没有新行:

<?xml version="1.0" encoding="UTF-8"?>   <url>     <lastmod>2020-08-22T15:35:47+03:00</lastmod>     <priority>1.00</priority>   </url>   <url>     <lastmod>2020-08-22T15:35:47+03:00</lastmod>     <priority>0.80</priority>   </url> </urlset>
2020-08-22T15:35:47+03:00 1.00 2020-08-22T15:35:47+03:00 0.80

我做错了什么?

我没有看“你做错了什么”,但这是另一种选择(你只需要调整日期的格式):

$xmlstarlet ed-u//lastmod-v“`date`”sitemap.xml
星期六8月22日14:51:59 CEST 2020
1
星期六8月22日14:51:59 CEST 2020
0.80
像这样:

$ xmlstarlet ed -u //lastmod -v "$(date --iso-8601=seconds)" sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
  <url>
    <lastmod>2020-08-22T14:53:46+02:00</lastmod>
    <priority>1.00</priority>
  </url>
  <url>
    <lastmod>2020-08-22T14:53:46+02:00</lastmod>
    <priority>0.80</priority>
  </url>
</urlset>
$xmlstarlet ed-u//lastmod-v“$(日期--iso-8601=seconds)”sitemap.xml
2020-08-22T14:53:46+02:00
1
2020-08-22T14:53:46+02:00
0.80

echo$sitemap
替换为
echo“$sitemap”

使用引号保留或追加换行符
Shell引用和扩展规则可能很棘手。您需要向sitemap引用您的作业。例如:

sitemap="$sitemap$line"
你可能还需要引用你的论点来回应。如果确实要在连接字符串的末尾追加换行符,则必须在引号外执行扩展,使用带有echo的
-e
标志,或者只执行另一个echo。您也可以使用printf。例如:

  • echo“${sitemap}${line}”$'\n'
  • echo-e“${sitemap}${line}\n”
  • printf“%s%s\n”“$sitemap”“$line”

如果您需要换行符,您可以将其移动到其他位置,但原则仍然相同。

感谢您的选择。但是我想通过gitbash在预提交git钩子中运行它。而且
gitbash
没有
xmlstarlet
LOL,它可用于Linux和windows,请参阅:Maybe。但是git bash说未找到
bash:xmlstarlet:command
您需要引用您对sitemap的分配。
sitemap="$sitemap$line"