xargsの-iと-Iの違い

xargsの-iと-Iの違い

わかりました。しかし、マニュアルとどう違うのか、マニュアルで使用されなくなったと言う理由が-iわかりません。たとえば、次の2行を使用することの違いは何ですか?-I-i

ls -t | tail -n 4 | xargs -I{} mv {} test2/

ls -t | tail -n 4 | xargs -i mv {} test2/

最初の行の場合、最初の行の前に何かを変更できますか{}-Iそれともこれは単なる構文ですか?

ありがとう

ベストアンサー1

廃止された理由は、管理者がxargs廃止する必要があると判断したためです。1.言葉があまりない。man xargs私のアーチシステムの関連部分は次のとおりです。

       -I replace-str
              Replace occurrences of  replace-str  in  the  initial-arguments
              with  names read from standard input.  Also, unquoted blanks do
              not terminate input items; instead the separator is the newline
              character.  Implies -x and -L 1.

       -i[replace-str], --replace[=replace-str]
              This  option  is  a synonym for -Ireplace-str if replace-str is
              specified.  If the replace-str argument is missing, the  effect
              is  the  same  as  -I{}.  This option is deprecated; use -I in‐
              stead.

-iしたがって、間の唯一の違いは、引数がないと次のように処理されること-Iです。文字列はこの種の仕事によく使われ、慣習になりました。たとえば、そのタスクでも同様の方法で使用されます。-i-I{}{}find{}-exec

      -exec command {} +
              This variant of the -exec action runs the specified command  on
              the  selected files, but the command line is built by appending
              each selected file name at the end; the total number of invoca‐
              tions  of  the  command  will  be  much less than the number of
              matched files.  The command line is built in much the same  way
              that xargs builds its command lines.  Only one instance of `{}'
              is allowed within the command, and it must appear at  the  end,
              immediately before the `+'; it needs to be escaped (with a `\')
              or quoted to protect it from interpretation by the shell.   The
              command  is executed in the starting directory.  If any invoca‐
              tion with the `+' form returns a non-zero value as exit status,
              then  find  returns a non-zero exit status.  If find encounters
              an error, this can sometimes cause an immediate exit,  so  some
              pending  commands  may  not  be  run  at  all.  For this reason
              -exec my-command ... {} + -quit may not  result  in  my-command
              actually being run.  This variant of -exec always returns true.

しかし、これは単なる慣例であり、xargs強制的に使用する必要はありません。{}xargs 'の代わりに何でも使用できます-I。たとえば、

$ seq 5 | xargs -I'&' echo "I read &"
I read 1
I read 2
I read 3
I read 4
I read 5

でも:

$ seq 5 | xargs -I'a' echo "I read a"
I re1d 1
I re2d 2
I re3d 3
I re4d 4
I re5d 5

したがって、文字-i列が与えられた場合、オプションは100%と等しいか、特定の-Iケースの略語です。つまり、明示的に指定したように使用することを-I{}意味します。xargs{}

$ seq 5 | xargs -i'a' echo "I read a"  ## as above
I re1d 1
I re2d 2
I re3d 3
I re4d 4
I re5d 5

$ seq 5 | xargs -i echo "I read {}"  ## implies -I{}
I read 1
I read 2
I read 3
I read 4
I read 5

$ seq 5 | xargs -I echo "I read {}"  ## breaks because -I requires a string
xargs: I read {}: No such file or directory

明らかに、管理者はxargsこれがもはや良い考えではないと判断したので、このオプションはもう廃止され、将来のリリースでは削除される可能性があります。


1言うことがもっとあるようです。エルクW.ミタグ現在削除されているコメントには、次の内容が記載されています。

この決定を下した人はxargs管理者だけではないことに注意してください。 -iはSUSv6(2004)でも「古い」として削除されました。 -I は SUS コアの一部ではありませんが、(オプション)XSI 拡張の一部としてのみ定義されます。したがって、保守的な標準の著者でさえ、ほぼ20年前のこの本は古すぎると思います。

おすすめ記事