Linux上ですべてのファイルを1つの引数として渡したいが、それはできない。xargsですべての引数をlinuxのコマンドの途中で渡す方法
これは
ls | sort -n | xargs -i pdftk {} cat output combinewd2.pdf
を働いているこれは、コマンドごとに単一の引数を渡しますが、私はすべて1つのコマンドで欲しいです。
Linux上ですべてのファイルを1つの引数として渡したいが、それはできない。xargsですべての引数をlinuxのコマンドの途中で渡す方法
これは
ls | sort -n | xargs -i pdftk {} cat output combinewd2.pdf
を働いているこれは、コマンドごとに単一の引数を渡しますが、私はすべて1つのコマンドで欲しいです。
このそれに
pdftk $(ls | sort -n) cat output combinewd2.pdf
または
pdftk `ls | sort -n` cat output combinewd2.pdf
をバッククォート使用を行うための一つの方法であるように、これは、スペースを含むファイル名では動作しませんコメントで指摘しました。その場合、あなたは二重引用符内のファイル名と、その後のevalの結果は、目的のコマンドになり、「0 foo」と「1つのバー」という名前の2つのファイルがあると仮定しeval
eval pdftk $(while IFS= read -r file; do
echo \"$file\"
done < <(ls | sort -n)) cat output combinewd2.pdf
を使用することができます。
をpdftk " 0 foo " " 1 bar " cat output combinewd2.pdf
ファイル名に改行が含まれている場合は、find
コマンドを使用してください。@ andrewdotnの答えのコメントに@joeytwiddleの説明を参照してください。 次のソリューションはまた、二重引用符をエスケープするsed
コマンドを使用して二重引用符でファイル名を処理します。
eval pdftk $(while IFS= read -r -d '' file; do
echo \"$file\"
done < <(find . -maxdepth 1 -type f -print0 | \
sed 's/"/\\"/g'| sort -zn)) cat output combinewd2.pdf
それは醜いですが、あなたはsh -c
を実行し、そのように、"${@}"
としてxargs
によって渡される引数のリストにアクセスできます。
ls | sort -n | xargs -d'\n' sh -c 'pdftk "${@}" cat output combinewd2.pdf' "${0}"
最後に余分な"${0}"
sh
man page says
など -cオプションが存在する場合は、まずは複雑な名前でその意志台無しに他のほとんどのソリューションをいくつかのファイルを作成してみましょう、これをテストします。ファイル名に改行が含まれていて、-c文字列が
、その後のコマンドが文字列から読み込まれます。 文字列の後に引数がある場合、それらは$ 0で始まる位置パラメータに割り当てられます。
$ seq 1 100 | xargs -I{} touch '{} with "spaces"' $ ls 1 with "spaces" 31 with "spaces" 54 with "spaces" 77 with "spaces" 10 with "spaces" 32 with "spaces" 55 with "spaces" 78 with "spaces" 100 with "spaces" 33 with "spaces" 56 with "spaces" 79 with "spaces" 11 with "spaces" 34 with "spaces" 57 with "spaces" 8 with "spaces" 12 with "spaces" 35 with "spaces" 58 with "spaces" 80 with "spaces" 13 with "spaces" 36 with "spaces" 59 with "spaces" 81 with "spaces" 14 with "spaces" 37 with "spaces" 6 with "spaces" 82 with "spaces" 15 with "spaces" 38 with "spaces" 60 with "spaces" 83 with "spaces" 16 with "spaces" 39 with "spaces" 61 with "spaces" 84 with "spaces" 17 with "spaces" 4 with "spaces" 62 with "spaces" 85 with "spaces" 18 with "spaces" 40 with "spaces" 63 with "spaces" 86 with "spaces" 19 with "spaces" 41 with "spaces" 64 with "spaces" 87 with "spaces" 2 with "spaces" 42 with "spaces" 65 with "spaces" 88 with "spaces" 20 with "spaces" 43 with "spaces" 66 with "spaces" 89 with "spaces" 21 with "spaces" 44 with "spaces" 67 with "spaces" 9 with "spaces" 22 with "spaces" 45 with "spaces" 68 with "spaces" 90 with "spaces" 23 with "spaces" 46 with "spaces" 69 with "spaces" 91 with "spaces" 24 with "spaces" 47 with "spaces" 7 with "spaces" 92 with "spaces" 25 with "spaces" 48 with "spaces" 70 with "spaces" 93 with "spaces" 26 with "spaces" 49 with "spaces" 71 with "spaces" 94 with "spaces" 27 with "spaces" 5 with "spaces" 72 with "spaces" 95 with "spaces" 28 with "spaces" 50 with "spaces" 73 with "spaces" 96 with "spaces" 29 with "spaces" 51 with "spaces" 74 with "spaces" 97 with "spaces" 3 with "spaces" 52 with "spaces" 75 with "spaces" 98 with "spaces" 30 with "spaces" 53 with "spaces" 76 with "spaces" 99 with "spaces" $ ls | sort -n | xargs -d'\n' sh -c 'set -x; pdftk "${@}" cat output combinewd2.pdf' "${0}" + pdftk '1 with "spaces"' '2 with "spaces"' '3 with "spaces"' '4 with "spaces"' '5 with "spaces"' '6 with "spaces"' '7 with "spaces"' '8 with "spaces"' '9 with "spaces"' '10 with "spaces"' '11 with "spaces"' '12 with "spaces"' '13 with "spaces"' '14 with "spaces"' '15 with "spaces"' '16 with "spaces"' '17 with "spaces"' '18 with "spaces"' '19 with "spaces"' '20 with "spaces"' '21 with "spaces"' '22 with "spaces"' '23 with "spaces"' '24 with "spaces"' '25 with "spaces"' '26 with "spaces"' '27 with "spaces"' '28 with "spaces"' '29 with "spaces"' '30 with "spaces"' '31 with "spaces"' '32 with "spaces"' '33 with "spaces"' '34 with "spaces"' '35 with "spaces"' '36 with "spaces"' '37 with "spaces"' '38 with "spaces"' '39 with "spaces"' '40 with "spaces"' '41 with "spaces"' '42 with "spaces"' '43 with "spaces"' '44 with "spaces"' '45 with "spaces"' '46 with "spaces"' '47 with "spaces"' '48 with "spaces"' '49 with "spaces"' '50 with "spaces"' '51 with "spaces"' '52 with "spaces"' '53 with "spaces"' '54 with "spaces"' '55 with "spaces"' '56 with "spaces"' '57 with "spaces"' '58 with "spaces"' '59 with "spaces"' '60 with "spaces"' '61 with "spaces"' '62 with "spaces"' '63 with "spaces"' '64 with "spaces"' '65 with "spaces"' '66 with "spaces"' '67 with "spaces"' '68 with "spaces"' '69 with "spaces"' '70 with "spaces"' '71 with "spaces"' '72 with "spaces"' '73 with "spaces"' '74 with "spaces"' '75 with "spaces"' '76 with "spaces"' '77 with "spaces"' '78 with "spaces"' '79 with "spaces"' '80 with "spaces"' '81 with "spaces"' '82 with "spaces"' '83 with "spaces"' '84 with "spaces"' '85 with "spaces"' '86 with "spaces"' '87 with "spaces"' '88 with "spaces"' '89 with "spaces"' '90 with "spaces"' '91 with "spaces"' '92 with "spaces"' '93 with "spaces"' '94 with "spaces"' '95 with "spaces"' '96 with "spaces"' '97 with "spaces"' '98 with "spaces"' '99 with "spaces"' '100 with "spaces"' cat output combinewd2.pdf
すべての引数が正しく引用される:
ls -v
が基本的に
ls | sort -n
である場合、これは失敗することに注意してください。
これは空白を含むファイル名で動作しますが、改行を含むファイル名では機能しません。これらはあまり一般的ではありませんが、以下のように正しく処理できます。 -type f -maxdepth 1 -print0 |ソート-zn | xargs -0 sh -c ... ' – joeytwiddle
もし' find'を使っているのであれば、 'xargs'はまったく必要ありません! [BashFAQ/020](http://mywiki.wooledge.org/BashFAQ/020)で推奨される 'find ... -exec [command] {} +'を使うことができます。 – joeytwiddle
@joeytwiddleファイル名に改行がある場合は 'ls'の代わりに' find'を使います。 – andrewdotn
これは(UNIXのファイルシステムで可能なすべてが)、スペース、改行、アポストロフィと引用符を含むファイル名で動作するはずです:あなたはあなたを知っていればやり過ぎ、受け入れ答えと比較されることがあります
find . -maxdepth 1 -type f -print0 |
sort -zn |
xargs -0 sh -c 'pdftk "[email protected]" cat output combinewd2.pdf' "$0"
単純なファイル名で作業しています。
しかし、今後再び使用されるスクリプトを作成する場合は、珍しい(ただし有効な)入力を満たしたときには爆発しないことが望ましいです。
これは基本的に、入力ファイルを改行ではなくゼロバイトで終了するようにして、1つ以上の改行文字を含むファイル名を保存するandrewdotnの回答です。
各オプション-print0
,-z
および-0
は、入力/出力をゼロバイトで区切る必要があることを各プログラムに伝えます。 3つの異なるプログラム、3つの異なる議論!
ここでの問題は、xargsが個々の引数を-i
と{}
というコマンドの途中に置くことができますが、xargsは複数の引数に対してこれを行うことを拒否します。これは私たちに多くのトラブルを引き起こす終わりの監視のように見えます!
上記の解決策とは別の方法として、ファイルの後に来る引数をファイルの最後に追加する方法もあります。
(
ls | sort -n
echo cat
echo output
echo combinewd2.pdf
) | xargs -d'\n' pdftk
このアプローチは改行を含むファイル名では動作しません。そのまれなケースでは、私の他の答えで提供されているように、0バイトで終了した行をxargsに渡すべきです。
使用-I
オプション:
echo prefix | xargs -I % echo % post
出力:
prefix post
しかし、これは望みどおりに機能しません(3つの引数で1回ではなく3回コマンドを呼び出します)入力として 'echo'ではなく' ls'を使います。これはOPがしようとしていることです... – DNA
非常 'bash'中心の答えは、それにもかかわらず、涼しいです。しかし、これは 'csh/tcsh'では動作しません。 (シェルの選択についてのコメントはすべて '/ dev/null'となります) – radical7
重大なことに、**これはスペースを含むファイル名では機能しません**。単語は別々の引数に分割されます。 – joeytwiddle
@joeytwiddle正しいですが、私は再度ejo – amdn