When a Scheme file is given to sagittarius
, it bounds an internal
variable to list of the remaining command-line arguments which you can get with
the command-line
procedure, then loads the Scheme program. If the first
line of scheme-file begins with "#!"
, then Sagittarius ignores the
entire line. This is useful to write a Scheme program that works as an
executable script in unix-like systems.
Typical Sagittarius script has the first line like this:
#!/usr/local/bin/sagittarius
or
#!/bin/env sagittarius
The second form uses "shell trampoline" technique so that the script works as
far as sagittarius
is in the PATH.
After the script file is successfully loaded, then Sagittarius will process all toplevel expression the same as Perl.
Now I show a simple example below. This script works like cat(1)
, without
any command-line option processing and error handling.
#!/usr/local/bin/sagittarius
(import (rnrs))
(let ((args (command-line)))
(unless (null? (cdr args))
(for-each (lambda (file)
(call-with-input-file file
(lambda (in)
(display (get-string-all in)))))
(cdr args)))
0)
If you need to add extra load path in the executing script file, you can also write like this:
#!/bin/bash
#|
# Adding lib directory located in the same directory as this script
# is located
DIR=$(dirname "$0")
exec sagittarius -L${DIR}/lib $0 "$@"
|#
;; Scheme script
(import (rnrs))
If the script file contains main
procedure, then Sagittarius execute
it as well with one argument which contains all command line arguments. This
feature is defined in
SRFI-22. So the
above example can also be written like the following:
#!/usr/local/bin/sagittarius
(import (rnrs))
(define (main args)
(unless (null? (cdr args))
(for-each (lambda (file)
(call-with-input-file file
(lambda (in)
(display (get-string-all in)))))
(cdr args)))
0)
NOTE: the main
procedure is called after all toplevel expressions
are executed.