尝试从存储在变量

时间:2019-11-23 15:07:32

标签: xml shell unix xmllint

我在存储在shell变量中的xml下面,如下所示:

   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属性值。

1 个答案:

答案 0 :(得分:1)

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

相反,您可以使用-告诉xmllint输入来自stdin,并使用“此处字符串”(<<<)来提供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)"