## 5.2 Writing Scheme scripts

- Since UNIX shells use the #! notation for starting scripts, the interpreter registers the special read-syntax `#!` as a comment and the reader ignores everything after the two characters up to the end of the current line.

  The easiest way is to use the `-script` option like this:

      % cat foo
      #! /usr/local/bin/csi -script
      (print (eval (with-input-from-string (car (command-line-arguments)) read)))

      % chmod +x foo
      % foo "(+ 3 4)"
      7

  The parameter `command-line-arguments` is set to a list of the parameters that were passed to the Scheme script. Scripts can be compiled to standalone executables (don't forget to declare used library units). Note that the compiler does not parse the extra arguments passed to a script via the `-script-meta` option!

  To overcome a limitation of UNIX that allows only a single argument to scripts, the `-script-meta` options is provided:

      % cat foo
      #! /usr/local/bin/csi -script-meta
      -case-sensitive
      (print (with-input-from-string (car (command-line-arguments)) read))

      % chmod +x foo
      % foo "FooBar"
      "FooBar"

- CHICKEN implements SRFI-22 and provides the Scheme script interpreters `scheme-r4rs, scheme-ieee-1178-1990, scheme-r5rs, scheme-srfi-0` and `scheme-srfi-7`. Scheme scripts can be compiled, the compiler determines language dialect from the invocation line:

      % cat bar
      #! /usr/bin/env scheme-r5rs
      (define (main args)
        (write (list->string (reverse (string->list (cadr args)))))
        (newline) )

      % chmod +x bar
      % bar "one two three"
      "eerht owt eno"
      % chicken bar -quiet
      % gcc bar.c `chicken-config -cflags -libs` -o cbar
      % cbar "one two three"}
      "eerht owt eno"

  Note: The Scheme script interpreters `scheme-r5rs` and `scheme-srfi-0` invoke the CHICKEN interpreter (`csi`) with the `-hygienic` option, which installs the highlevel macro definitions. This will result in slightly longer startup times for these scripts than for, say, scripts invoked via `scheme-r4rs`. Compiling the script to native code will of course eliminate the problem.

  The additional script interpreters `scheme-chicken-hygienic` and `scheme-chicken` are provided, which run `csi` in normal mode, with or without hygienic macros.

  Compiled scheme scripts that invoke `scheme-chicken, scheme-chicken-hygienic` or `csi -script` are automatically linked with all library modules that are normally available in the interpreter.

  What follows is a list that shows what options are passed to `csi` when the respective script interpreter is invoked:

  ``

  scheme-chicken |
  scheme-chicken-hygienic | -hygienic
  scheme-r4rs | -strict
  scheme-r5rs | -strict -hygienic
  scheme-srfi-0 | -strict-srfi-0 -hygienic
  scheme-srfi-7 | -strict -srfi-7 -hygienic
  scheme-ieee-1178-1990 | -strict

  (All script interpreters pass `-no-init -quiet -batch`)

  For more information, see the SRFI-22 document 4

- Windows and DOS:

  CHICKEN supports writing shell scripts in Scheme for these platforms as well, using a slightly different approach. The first example would look like this on Windows:

      C:>type foo.bat
      @;csibatch %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
      (print (eval (with-input-from-string (car (command-line-arguments)) read)))

      C:>foo "(+ 3 4)"
      7

  Like UNIX scripts, batch files can be compiled.

4 http://srfi.schemers.org/srfi-22/srfi-22.html