GNU/Linux >> Linux の 問題 >  >> Linux

Linuxでコマンドの途中でxargsを使用してすべての引数を渡すにはどうすればよいですか

-I を使用 オプション:

echo prefix | xargs -I % echo % post

出力:

prefix post

これは 1 つの方法です

pdftk $(ls | sort -n) cat output combinewd2.pdf

またはバッククォートを使用

pdftk `ls | sort -n` cat output combinewd2.pdf

たとえば、ファイル名が 100、2、9、3.14、10、1 の場合、コマンドは次のようになります

pdftk 1 2 3.14 9 10 100 cat output combinewd2.pdf

スペースやその他の特殊文字を含むファイル名を処理するには、これを 修正 してください @joeytwiddle の素晴らしいバージョン 回答 (数値順にソートされません。以下の説明を参照してください):

#-- The following will handle special characters, and
#   will sort filenames numerically
#   e.g. filenames 100, 2, 9, 3.14, 10, 1 results in 
#      ./1 ./2 ./3.14 ./9 ./10 ./100
#
find . -maxdepth 1 -type f -print0 |
  sort -k1.3n -z -t '\0' |
  xargs -0 sh -c 'pdftk "[email protected]" cat output combinewd2.pdf' "$0"

xargs の代替 (bash 固有)

xargs は外部コマンドで、前の例では sh を呼び出します 次に pdftk を呼び出します .

別の方法は、組み込みの mapfile を使用することです 利用可能な場合、または位置パラメータを使用します。次の例では、print0_files という 2 つの関数を使用しています。 NUL で終了するファイル名と create_pdf を生成します pdftk を呼び出す :

print0_files | create_pdf combinewd2.pdf

関数は次のように定義されます

#-- Generate the NUL terminated filenames, numerically sorted
print0_files() {
    find . -maxdepth 1 -type f -print0 |
        sort -k1.3n -z -t '\0'
}
#-- Read NUL terminated filenames using mapfile
create_pdf() {
    mapfile -d ''
    pdftk "${MAPFILE[@]}" cat output "$1"
}
#-- Alternative using positional parameters
create_pdf() {
    local -r pdf=$1
    set --
    while IFS= read -r -d '' f; do set -- "[email protected]" "$f"; done
    pdftk "[email protected]" cat output "$pdf"
}

ディスカッション

コメントで指摘されているように、単純な最初の回答は、スペースやその他の特殊文字を含むファイル名では機能しません。

#-- The following will not sort numerically due to ./ prefix,
#   e.g. filenames 100, 2, 9, 3.14, 10, 1 results in 
#      ./1 ./10 ./100 ./2 ./3.14 ./9
#
find . -maxdepth 1 -type f -print0 |
  sort -zn |
  xargs -0 sh -c 'pdftk "[email protected]" cat output combinewd2.pdf' "$0"

各ファイル名の前に ./ が付いているため、数値順に並べ替えられません findまで 指図。 find のいくつかのバージョン コマンドサポート -printf '%P\0' ./ を含まない プレフィックス。より簡単で移植可能な修正は、 -d, --dictionary-order を追加することです sort へのオプション コマンドを変更して、比較で空白と英数字のみを考慮しますが、それでも間違った順序が生成される可能性があります

#-- The following will not sort numerically due to decimals
#   e.g. filenames 100, 2, 9, 3.14, 10, 1 results in 
#      ./1 ./2 ./9 ./10 ./100 ./3.14
#
find . -maxdepth 1 -type f -print0 |
  sort -dzn |
  xargs -0 sh -c 'pdftk "[email protected]" cat output combinewd2.pdf' "$0"

ファイル名に小数が含まれていると、数値の並べ替えが正しく行われない可能性があります。 sort コマンドはソート時にフィールドへのオフセットを許可します sort -k1.3n ただし、ファイル名をできるだけ一般的なものにする場合は、フィールド セパレータの定義に注意する必要があります。幸い sort -t '\0' フィールド区切り文字として NUL を指定し、find -print0 オプションは、ファイル名間の区切り文字として NUL が使用されることを示しているため、sort -z -t '\0' は、レコード区切り文字とフィールド区切り文字の両方として NUL を指定します。これにより、各ファイル名は単一のフィールド レコードになります。それを考えると、単一のフィールドにオフセットして ./ をスキップできます 1 番目のフィールドの 3 番目の文字を数値ソートの開始位置として指定する接頭辞 sort -k1.3n -z -t '\0' .


これは、スペース、改行、アポストロフィ、および引用符を含むファイル名で機能するはずです (これらはすべて UNIX ファイルシステムで可能です):

find . -maxdepth 1 -type f -print0 |
  sort -zn |
  xargs -0 sh -c 'pdftk "[email protected]" cat output combinewd2.pdf' "$0"

単純なファイル名で作業していることがわかっている場合、これは受け入れられている回答と比較してやり過ぎかもしれません。

しかし、将来再び使用されるスクリプトを作成している場合は、異常な (しかし有効な) 入力に遭遇したときに爆発しないことが望ましいです。

これは基本的に、入力ファイルを改行ではなくゼロバイトで終了する andrewdotn の回答の適応であるため、1 つ以上の改行文字を含むファイル名を保持します。

それぞれのオプション -print0-z そして -0 入力/出力をゼロバイトで区切る必要があることを各プログラムに伝えます。 3 つの異なるプログラム、3 つの異なる引数!


見苦しいですが、 sh -c を実行できます xargs によって渡された引数のリストにアクセスします "${@}" として 、そのように:

ls | sort -n | xargs -d'\n' sh -c 'pdftk "${@}" cat output combinewd2.pdf' "${0}"

余分な "${0}" 最後にあるのは sh のように マニュアルページには

<ブロック引用>

-c 文字列

-c の場合 オプションが存在する場合、コマンドは string から読み取られます . string の後に引数がある場合 、$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 です .


Linux
  1. LinuxDDコマンド-すべてのオプションを使用した15の例

  2. Linux でロックされているすべてのユーザーを一覧表示するにはどうすればよいですか?

  3. コマンド出力を複数の引数として別のコマンドに渡す方法

  1. Linuxでの並べ替えコマンドと例

  2. LinuxでSCPコマンドにパスワードを渡す方法

  3. 同じ引数で別のコマンドを実行するにはどうすればよいですか?

  1. Linuxのソートコマンドと例

  2. 例を使用してLinuxでIPコマンドを使用する方法

  3. Linuxでxargsを使用してファイルを移動するにはどうすればよいですか?