spacepaste

  1.  
  2. #!/usr/bin/env sh
  3. # -*- scheme -*-
  4. exec guile -L $(dirname $(dirname $(realpath "$0"))) -e '(@@ (examples pipe) main)' -s "$0" "$@"
  5. ; !#
  6. ;; Use pipes to connect several commands
  7. (define-module (examples pipe)
  8. #:export (!))
  9. (import (ice-9 popen)
  10. (ice-9 rdelim)
  11. (only (srfi srfi-1) fold)
  12. (ice-9 pretty-print))
  13. (define (! . cmds)
  14. "A pipe-procedure: connect each of the CMDS with the next
  15. in the list, the first with stdin and the last with stdout."
  16. (define (read-till-eof in-port)
  17. (read-delimited "" in-port))
  18. (define (connect B A)
  19. (let
  20. ((in-port (if (port? A) A (open-input-pipe A)))
  21. (out-port (open-input-output-pipe B)))
  22. (pretty-print (cons A B))
  23. (let ((data (read-till-eof in-port)))
  24. (pretty-print data)
  25. (display data out-port))
  26. (close in-port)
  27. out-port))
  28. (fold connect (car cmds) (cdr cmds)))
  29. (define (main args)
  30. (display (read-delimited "" (! "echo 1" "echo 2" "echo 3" "echo 4"))))
  31.