You can execute JScheme scripts directly on UNIX by using a first line like:
#! /usr/bin/env runjschemeThe code is executed and the command line arguments are in the vector ARGS. Here are some simiple examples:
scriptometer.zip is a .zip archive of this directory.
""
(display "Hello World\n")
(print ARGS)
(load "using/run.scm")
(define getvar
(let ((table (Properties.)))
(for-each*
(lambda (r) (let ((it (.split r "=")))
(.put table (vector-ref it 0) (vector-ref it 1))))
(BufferedReader (inputReader (if (.startsWith ($ "os.name") "Windows")
(run (cmd cmd /c set))
(run (cmd /bin/sh set))))))
(lambda (name) (.get table name))))
(print (getvar "HOME"))
(if (not (.exists (java.io.File. "/etc/mtab"))) (System.exit 1))
(if (not (.canWrite (java.io.File. "/etc/mtab"))) (System.exit 1))
(let ((a 1)
(b 2))
(print {[a] + [b] = [(+ a b)]}))
(load "elf/run.scm") (define (system . args) ; More like Python. (out (run (cmd ,@args)))) (if (system "false") (system "echo" "false failed") (system "echo" "done"))
(load "using/run.scm")
(import "java.io.File")
(let ((file (vector-ref ARGS 1)))
(let ((out (File.createTempFile "foo" "bar")))
(print out)
(call-with-output-file out
(lambda (s)
(for-each* (lambda (r) (.println s (.replaceFirst r "#.*" "")))
(BufferedReader (File. file)))
(.close s)))
(.renameTo out (File. file))))
(import "java.io.File")
(define (s->o srcDir classDir fromtype totype)
;; Source file to object file converter.
(lambda (file)
;; Converts a .java file in srcDir into a .class file in classDir.
(let ((f (.toString (relativize srcDir file))))
(File. classDir
(string-append (.substring f 0
(- (.length f) (.length fromtype)))
totype)))))
(define (needsUpdate? s->o)
(lambda (jf)
;; Does .java file jf need to be recompiled?
(let ((cf (s->o jf)))
(or (not (.exists cf)) (<= (.lastModified cf) (.lastModified jf))))))
(let* ((dir (java.io.File. "."))
(s->o (s->o dir dir ".c" ".o"))
(update? (needsUpdate? s->o)))
(for-each (lambda (c)
(if (update? c)
(let ((o (s->o c)))
(display {compiling [c] to [o]\n})
(out (run (cmd gcc -c -o ,c ,o))))))
(files* (java.io.File. ".") (isFile ".c"))))
(load "using/run.scm")
(define (any p xs) (and (pair? xs) (or (p (car xs)) (any p (cdr xs)))))
(define (forLines f files)
(for-each (lambda (file) (for-each* f (BufferedReader (File. file))))
files))
(define (show x) (display x) (newline))
(define (grep-F -i re files)
(let ((lines (vector->list (.split re "\n")))
(f (if -i .equalsIgnoreCase .equals)))
(forLines (lambda (r) (if (any (lambda (p) (f p r)) lines) (show r)))
files)))
(define (grep re files)
(let ((p (Pattern.compile re)))
(forLines (lambda (r) (if (.find (.matcher p r)) (show r)))
files)))
(let* ((args (cdr (vector->list ARGS)))
(-F (member "-F" args))
(-i (member "-i" args))
(-h (member "-h" args))
(args (filter (lambda (a) (not (.startsWith a "-"))) args))
(re (if -i {[(?i)][(car args)]} (car args)))
(files (cdr args)))
(cond ((or -h (null? args))
(display {usage: grep [-F] [-i] regexp file ...\n}))
(-F (grep-F -i re files))
(else (grep re files))))