
2つのUbuntuインストールがあります。
ㅏ:14.04.3 LTS (GNU/Linux 3.13.0-77-generic x86_64)
第二:14.04.2 LTS (GNU/Linux 3.13.0-61-generic x86_64)
次のコマンドを実行しています。
find . -name "nosuch.file" -print0 | xargs -0 ls
(実際のコマンドはより複雑ですが、この程度に絞り込みました。)nosuch.file
現在、ディレクトリやサブディレクトリにファイルはありません。
サーバーAでは、このコマンドは期待どおりに空の出力を生成します。
サーバーBでは、このコマンドは現在のディレクトリ(コマンドを実行しているディレクトリ)のリストを生成します。
両方のサーバーで実行すると、空のfind . -name "nosuch.file" -print0
出力が生成されます。
両方のサーバーに同じバージョンがインストールされていますfind
。xargs
ls
$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)
$ xargs --version
xargs (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
$ ls --version
ls (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Richard M. Stallman and David MacKenzie.
コマンドが異なる動作をするのはなぜですか?サーバーAのように一貫した動作を達成するにはどうすればよいですか(ファイルが見つからない場合は空の出力)。
答え1
「サーバーAのように一貫した動作を達成するにはどうすればよいですか(ファイルが見つからない場合は空の出力)?」
この動作を実行するには、/オプションを使用して、指定されたコマンドがファイル名を渡さずに実行されないようにするxargs
ことができます。 ~から-r
--no-run-if-empty
マニュアルページ:
標準入力に空白以外の項目がない場合は、コマンドを実行しないでください。通常、コマンドは入力がなくても一度だけ実行されます。
このオプションはGNU機能であり、xargs
標準ではありません。つまり、POSIXで指定します。