2012年4月3日火曜日

外部プロセスの標準出力を取得する


1 Racket

 1:  ;; racket
2: #!lang racket
3:
4: (define (system->string-list cmd . args)
5: (match-define
6: (list in out pid err procedure)
7: (apply process* cmd args))
8: (procedure 'wait)
9: (unless (eq? (procedure 'status) 'done-ok)
10: (raise 'exit-failure))
11: (port->lines in))
12:
13: ;; (sytem->string-list "/bin/ls")

2 Chicken

1:  ;; Chicken
2: (require 'posix)
3:
4: (call-with-input-pipe "ls"
5: (lambda (p)
6: (read-lines p)))

3 Gauche

1:  ;; Gauche
2: (use gauche.process)
3: (process-output->string-list '(ls))

4 Guile

1:  ;; Guile
2: (use-modules (ice-9 popen))
3: (use-modules (ice-9 rdelim))
4:
5: (let ((p (open-input-pipe "ls")))
6: (let ((lines (read-delimited "" p)))
7: (close-pipe p)
8: (string-split lines #\newline)))

5 Ruby

1:  `ls`.split

6 Python

1:  import subprocess
2: p = subprocess.Popen("ls", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
3: close_fds=True)
4:
5: p.stdout.readlines()

0 件のコメント:

コメントを投稿