xargs

きっとみんな大好きxargs

いまさらxargsの便利さを主張してみる
http://openlab.dino.co.jp/2008/02/20/133431188.html
xargs - 知らないと損する大変便利なコマンド
http://www.syboos.jp/linux/doc/xargs-command.html

gaucheで書いてみる

myxargs.scm

(use gauche.process)
(use file.util)

(define (myxargs path command)
  (cond ( (not (file-exists? path))
          (print path " not found."))
        ((file-is-directory? path)
          (run-process (append command (list path)) :wait #t)
          (for-each (lambda (l) (myxargs l command)) (directory-list path :add-p
ath? #t :children? #t)))
        (else
          (run-process (append command (list path)) :wait #t))
  )
)


find+xargsとの比較

echo

$ find ./test | xargs echo
./test ./test/b ./test/b/e.txt ./test/b/f.txt ./test/c ./test/c/fibo.c ./test/a ./test/a/d.txt
$ rlwrap gosh
gosh> (load "./scripts/myxargs.scm")
#t
gosh> (myxargs "./test" '(echo))
./test
./test/a
./test/a/d.txt
./test/b
./test/b/e.txt
./test/b/f.txt
./test/c
./test/c/fibo.c
#


wc -l

$ find ./test | xargs wc -l
wc: ./test: ディレクトリです
      0 ./test
wc: ./test/b: ディレクトリです
      0 ./test/b
      9 ./test/b/e.txt
      0 ./test/b/f.txt
wc: ./test/c: ディレクトリです
      0 ./test/c
     23 ./test/c/fibo.c
wc: ./test/a: ディレクトリです
      0 ./test/a
      1 ./test/a/d.txt
     33 合計
...
gosh> (myxargs "./test" '(wc -l))
wc: ./test: ディレクトリです
0 ./test
wc: ./test/a: ディレクトリです
0 ./test/a
1 ./test/a/d.txt
wc: ./test/b: ディレクトリです
0 ./test/b
9 ./test/b/e.txt
0 ./test/b/f.txt
wc: ./test/c: ディレクトリです
0 ./test/c
23 ./test/c/fibo.c
#


nkf --guess

$ find ./test | xargs nkf --guess
./test: ASCII
./test/b: ASCII
./test/b/e.txt: EUC-JP (LF)
./test/b/f.txt: ASCII
./test/c: ASCII
./test/c/fibo.c: ASCII (LF)
./test/a: ASCII
./test/a/d.txt: ASCII (LF)
...
gosh> (myxargs "./test" '(nkf --guess))
ASCII
ASCII
ASCII (LF)
ASCII
EUC-JP (LF)
ASCII
ASCII
ASCII (LF)
#

改善の余地は多いなぁ。。


参照したURL:
http://practical-scheme.net/gauche/man/gauche-refj_92.html