ファイルに格納されている文字列をコマンドの引数リストとして提供しますか?

ファイルに格納されている文字列をコマンドの引数リストとして提供しますか?

次のテキストファイルにURLリストをインストールしましたmyurls

http://www.examples.com/1
http://www.examples.com/2
http://www.examples.com/3

wkhtmltopdfこれらのURLを入力に渡すにはどうすればよいですか?

ファイルを使わずにURLを保存する簡単な方法は次のとおりです。

wkhtmltopdf http://www.examples.com/1 http://www.examples.com/2 http://www.examples.com/3 all.pdf

おそらく、wkhtmltopdf買収に関する特別な要件があるかもしれませんが、私の質問は次のようになります wkhtmltopdf。ファイルに保存されている(改行で区切られた)文字列のリストをコマンドの引数リストとして提供する方法は?

ベストアンサー1

努力する:

# 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"

通常、適用されるプロパティを変更した後にすべてのシェル状態を復元するときは注意してください。しかし、基本原則はシェルの分離台を$IFS中に入れておくだけです。いいえコマンド置換の拡張にが含まれている場合は[?*globに展開し、引用符なしで引数リストに展開します。

副作用を受ける必要がないため、サブシェルでより簡単で強力に実行できます。

(   set -f; IFS='
';  wkhtmltopdf $(cat) allurl.pdf
)   <myurls

おすすめ記事