尝试从存储在变量中的 xml 中获取详细信息时观察到错误

Observed error while trying to fetch details from xml that stored in variable

我有以下 xml 存储在 shell 变量中,如下所示:

   file1=$(sudo curl --silent  -XGET 'https://<username>:<token>@dev-pilot.us-central1.gcp.dev.amazon.com/te-ci-9414/job/teserv-ci-9414_FSS_functional_test_develop-test_2/Report_20for_20FS/testng-results.xml')

   echo $file1  // It prints expected xml file.

//尝试从上面的变量中获取@Total,如下所示:

   total=$(xmllint --xpath "/testng-results/@total" $file1 |sed 's/"//g' | sed 's/^/"/' |sed 's/$/"/')

但它返回以下错误消息:

   20:27:14 /tmp/jenkins4105200679331055571.sh: line 22: /usr/bin/xmllint: Argument list too long

xml 文件太大,所以我刚刚提到了一篇我正在查看该文件的文章:

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="7" total="2952" passed="2945">

任何人都可以帮助获取@total 属性值。

当您在 xmllint 命令行中使用 $file1 时,xmllint 认为您的 xml 是一堆命令行参数。

您可以做的是使用 - 告诉 xmllint 输入来自标准输入,并使用 "Here String" (<<<) 来提供您的 XML...

(注意:在 Windows 上使用 cygwin 进行测试。)

#!/usr/bin/env bash.exe

file1='<testng-results skipped="0" failed="7" total="2952" passed="2945"/>'

total=$(xmllint --xpath "/testng-results/@total" - <<<"$file1")

echo $total

输出:

total="2952"

我不确定你的 sed 管道正在完成什么,但如果你只想要值 (2952),你可以在你的 xpath 中使用 string()...

--xpath "string(/testng-results/@total)"