3

我正在尝试编写一个脚本,该脚本将浏览一个目录,找到所有 XML 文件,运行它们xmllint,并将格式化的结果保存到名为formatted. 这是我到目前为止的脚本:

find . -maxdepth 1 -type f -iname "*.xml" | xargs -I '{}' xmllint --format '{}' > formatted/'{}'

这在一定程度上有效。子目录以一个名为 的文件结束"{}",它只是通过 处理的最终文件的结果xmllint。如何让文件正确写入子目录?

4

1 回答 1

10

您看到的命名文件{}可能应该包含所有格式化文件。这样做的原因是您使用的重定向实际上并不是xargs看到的命令的一部分。重定向由 shell 解释,所以它的作用是运行

find . -maxdepth 1 -type f -iname "*.xml" | xargs -I '{}' xmllint --format '{}'

并将输出保存到名为formatted/{}.

尝试使用--output选项xmllint而不是重定向:

... | xargs -I '{}' xmllint --format '{}' --output formatted/'{}'

您还可以xargs使用以下-exec选项避免调用find

find . -maxdepth 1 -type f -iname "*.xml" -exec xmllint --format '{}' --output formatted/'{}' \;
于 2013-12-13T22:47:42.860 回答