綾小路龍之介の素人思考

[xargs] 標準入力の1行に対してコマンドを実行する

標準出力のリダイレクトによって、あるコマンドへの標準入力にすることは|を使うことで可能。しかし、例えばテキストファイルに書かれた内容の各行の内容を引数にしてコマンドを実行するにはどうすればいいのだろう。答えはxargs --max-args 1。

例えばdeborphanは使われていないと思われるパッケージを検索して標準出力に表示してくれる。これの各行をaptitude whyへのコマンドライン引数としたい。

# deborphan --guess-all
libgsl0-dev
libmail-imapclient-perl
libmail-pop3client-perl
libunicode-japanese-perl
libsigc++-dev
libio-compress-base-perl
libsigc++-2.0-dev
libio-compress-zlib-perl
libcompress-raw-zlib-perl
libperl-dev
libidn11-dev
python-dev
liblua5.1-0-dev
libmime-perl
libclass-factory-util-perl
libdatetime-format-strptime-perl
libwww-mechanize-shell-perl
libgtk2.0-dev
libdatetime-format-mail-perl
libaspell-dev
libtest-exception-perl
liblocale-maketext-lexicon-perl
libsoap-lite-perl
libdatetime-format-w3cdtf-perl
libncursesw5-dev
libxml-sax-writer-perl
comerr-dev
libgnutls-dev
libxml-twig-perl
libtest-www-selenium-perl
libjson-perl
netcat
ruby1.8-dev

以下のようにしてxargsにリダイレクトすれば良い。xargs --max-args 1とすることで、xargsは標準入力を受け取り、これの各行を引数としてaptitude whyに与える。

# deborphan --guess-all | xargs --max-args 1 aptitude why
Unable to find a reason to install libgsl0-dev.
i   debhelper        Depends  po-debconf
i A po-debconf       Suggests libmail-box-perl
p   libmail-box-perl Suggests libmail-imapclient-perl (>= 3)
Unable to find a reason to install libmail-pop3client-perl.
Unable to find a reason to install libunicode-japanese-perl.
Unable to find a reason to install libsigc++-dev.
Unable to find a reason to install libio-compress-base-perl.
i   libsigc++-2.0-dev Suggests   libsigc++-2.0-doc
p   libsigc++-2.0-doc Recommends libsigc++-2.0-dev
Unable to find a reason to install libio-compress-zlib-perl.
i   libio-compress-zlib-perl Depends libio-compress-perl
p   libio-compress-perl      Depends libcompress-raw-zlib-perl (>= 2.024)
Unable to find a reason to install libperl-dev.
i   mercurial           Suggests qct
p   qct                 Depends  mercurial | git-core | bzr | monotone | subversion | cvs
p   bzr                 Suggests python-pycurl
p   python-pycurl       Suggests libcurl4-gnutls-dev
p   libcurl4-gnutls-dev Depends  libidn11-dev
i   installation-report Recommends reportbug
p   reportbug           Suggests   python-gtk2
p   python-gtk2         Depends    python-numpy (< 1:1.5)
p   python-numpy        Suggests   python-dev
Unable to find a reason to install liblua5.1-0-dev.
Unable to find a reason to install libmime-perl.
Unable to find a reason to install libclass-factory-util-perl.
Unable to find a reason to install libdatetime-format-strptime-perl.
Unable to find a reason to install libwww-mechanize-shell-perl.
i   wl         Suggests   mhc
p   mhc        Recommends mhc-utils
p   mhc-utils  Depends    libpisock9
p   libpisock9 Suggests   evolution
p   evolution  Suggests   bug-buddy
p   bug-buddy  Recommends gnome-dbg
p   gnome-dbg  Depends    anjuta-dbg
p   anjuta-dbg Depends    anjuta (= 2:2.32.0.0-5)
p   anjuta     Suggests   libgtk2.0-dev
i   liblocale-maketext-lexicon-perl Suggests libtemplate-perl (>= 2.20)
p   libtemplate-perl                Suggests libtemplate-plugin-xml-perl
p   libtemplate-plugin-xml-perl     Depends  libxml-rss-perl
p   libxml-rss-perl                 Depends  libdatetime-format-mail-perl
Unable to find a reason to install libaspell-dev.
Unable to find a reason to install libtest-exception-perl.
Unable to find a reason to install liblocale-maketext-lexicon-perl.
i   cdbs       Suggests   devscripts
p   devscripts Recommends libsoap-lite-perl
i   liblocale-maketext-lexicon-perl Suggests libtemplate-perl (>= 2.20)
p   libtemplate-perl                Suggests libtemplate-plugin-xml-perl
p   libtemplate-plugin-xml-perl     Depends  libxml-rss-perl
p   libxml-rss-perl                 Depends  libdatetime-format-w3cdtf-perl
i   aptitude       Depends  libcwidget3
i A libcwidget3    Suggests libcwidget-dev
p   libcwidget-dev Depends  libncursesw5-dev
Unable to find a reason to install libxml-sax-writer-perl.
i   debconf        Suggests libqtgui4-perl
p   libqtgui4-perl Depends  libqt4-sql (>= 4:4.5.3)
p   libqt4-sql     Suggests libqt4-dev
p   libqt4-dev     Suggests libpq-dev
p   libpq-dev      Depends  comerr-dev
i   debconf        Suggests libqtgui4-perl
p   libqtgui4-perl Depends  libqt4-sql (>= 4:4.5.3)
p   libqt4-sql     Suggests libqt4-dev
p   libqt4-dev     Suggests unixodbc-dev
p   unixodbc-dev   Suggests libqt3-mt-dev
p   libqt3-mt-dev  Depends  libcups2-dev
p   libcups2-dev   Depends  libgnutls-dev
i   r-base                Depends    r-base-core (>= 2.11.1-6)
i A r-base-core           Depends    xdg-utils
i A xdg-utils             Recommends iceweasel | www-browser
p   galeon                Provides   www-browser
p   galeon                Recommends gnome-control-center
p   gnome-control-center  Recommends gnome-session
p   gnome-session         Depends    gnome-panel (>= 2.26)
p   gnome-panel           Recommends gnome-applets (>= 2.12.1-1)
p   gnome-applets         Depends    liboobs-1-4 (>= 2.30.0)
p   liboobs-1-4           Depends    system-tools-backends (>= 2.9.2)
p   system-tools-backends Depends    libnet-dbus-perl
p   libnet-dbus-perl      Depends    libxml-twig-perl
Unable to find a reason to install libtest-www-selenium-perl.
i   cdbs       Suggests   devscripts
p   devscripts Recommends libjson-perl
i   cdbs               Suggests   devscripts
p   devscripts         Recommends lsb-release
i A lsb-release        Suggests   lsb
p   lsb                Suggests   lsb-printing
p   lsb-printing       Depends    foomatic-filters
p   foomatic-filters   Recommends foomatic-db-engine (>= 4.0)
p   foomatic-db-engine Recommends netcat
i   ruby     Suggests ruby-dev
p   ruby-dev Depends  ruby1.8-dev

リファレンス

  1. Debian パッケージ管理
  2. 第2章 Debian パッケージ管理
  3. Debianリファレンス
  4. "aptitude why" - Google 検索
  5. D: AptGet - Debian GNU/Linux スレッドテンプレ
  6. "aptitude --without-recommend"|"aptitude -R" - Google 検索
  7. Manpage of XARGS
  8. manpage xargs - Google 検索
  9. 標準入力の値を引数として実行する
  10. ログファイルをまとめて解凍 | 渋谷生活
  11. xargsの便利なオプション, - yuu_nkjm blog(2009-12-08)
  12. 確認後、入力のそれぞれの行のコマンドを実行xargsは

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2010-01-19T10:45:14+09:00
  2. Modified: 2010-01-19T10:45:14+09:00
  3. Generated: 2017-03-03T23:09:16+09:00