0

Trying to read .xml file from another server into a variable

test=$(ssh root@server "cat /dir/file.xml")

But when doing echo $test, it prints the .xml file in one-liner format.

Then trying to execute xmllint on this file,

xmllint $test

and getting the following error on each tag.

warning: failed to load external entity "..."

Note: when doing the same procedure, but on local .xml file(without ssh), it's working and getting the expected result.

Any way to solve this? Or use xmllint tool remotely?

2

Note that your $test variable contains the content of the xml file, not its path.

Anyway, to show it keeping the spaces intact, quote the variable: echo "$test".

To run a program getting the input from variable you can use stdin redirecton, in this case it would be:

echo "$test" | xmllint -

You can also avoid using a variable:

ssh root@server "cat /dir/file.xml" | xmllint -

Finally, in bash, you can also use process substitution (see man bash):

xmllint <(ssh root@server "cat /dir/file.xml")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.