Tim*_*Tim 4 shell xargs stdin arguments
我在名为 的文本文件中安装了 URL 列表myurls
:
http://www.examples.com/1
http://www.examples.com/2
http://www.examples.com/3
Run Code Online (Sandbox Code Playgroud)
我该如何将这些 URL 作为wkhtmltopdf
输入传递?
不使用文件存储 URL 的直接方法是
wkhtmltopdf http://www.examples.com/1 http://www.examples.com/2 http://www.examples.com/3 all.pdf
Run Code Online (Sandbox Code Playgroud)
也许wkhtmltopdf
对其参数有特殊要求,但我认为我的问题可能比 wkhtmltopdf
:如何提供存储在文件中的(换行符分隔的)字符串列表作为命令的参数列表?
尝试:
# disable shell filename generation (globbing)
# and temporarily save applicable shell state
set -f -- "-${-:--}" "${IFS+IFS=\$2;}" "$IFS" "$@"
# explicitly set the shell's Internal
# Field Separator to only a newline
eval "IFS='$(printf \\n\')"
# split command substitution into an
# arg array at $IFS boundaries while
# eliding all blank lines in myurls
wkhtmltopdf $(cat <myurls) allurl.pdf
# restore current shell to precmd state
unset IFS; set +f "$@"; eval "$1 shift 2"
Run Code Online (Sandbox Code Playgroud)
在可能更改普遍应用的属性后恢复所有 shell 状态时要格外小心。但基本规则只是将 shell 的拆分器设置为$IFS
,注意不要在任何命令替换的扩展包含 的情况下进行 glob [?*
,然后将其不加引号地扩展为参数列表。
它可以在子 shell 中更简单、更稳健地完成,因为您不必承受任何后遗症:
( set -f; IFS='
'; wkhtmltopdf $(cat) allurl.pdf
) <myurls
Run Code Online (Sandbox Code Playgroud)