single line alternative Marc Feeley (09 Mar 2001 05:16 UTC)
Re: single line alternative Per Bothner (09 Mar 2001 05:43 UTC)

single line alternative Marc Feeley 09 Mar 2001 05:16 UTC

I have used the following approach for scripts for the Gambit interpreter:

  #! /bin/sh
  :;exec gsi -f -e "(define : 0)" "$0" "$*"
  (write "hello world")
  (newline)

Note the use of the prefix

  :;

on the second line.  When the shell encounters it, the colon does
nothing and then the command after the semicolon is processed, which
invokes the Gambit interpreter and after defining the variable ``:''
to a dummy value causes it to load the script as Scheme source code.
The interpreter will ignore the first line because it starts with the
Scheme datum ``#!'' (which is called the ``script'' object) and the
second line does nothing (a useless reference to the variable ``:''
followed by a Scheme comment) and then evaluation proceeds with the
rest of the file.

Note that the (define : 0) trick could be avoided by predefining this
variable in the runtime system, or using the following prefix instead

  ":";

I like Eli Barzilay's #| ... |# proposal:

  #! /bin/sh
  #|
  exec gsi -f "$0" "$*"
  |#
  (write "hello world")
  (newline)

which cleanly separates the Scheme and shell commands and only
requires special treatment of the first line if it starts with ``#!''
and support for #|...|# comments.  It is unfortunate however that
``#!'' and ``#|'' are visually similar, which is bound to cause some
confusion.

Anyway, the point of my message is to request that a space be added
after the ``#!''.  I think this is valid under UNIX (at least it works
fine on the many systems I have used).  It allows the reader to
distinguish the various objects that start with ``#!'' in Gambit:

   #!          script object
   #!eof       end-of-file object
   #!optional  optional object
   #!rest      rest object
   #!key       key object

The last 3 exist in DSSSL (for marking optional and keyword
parameters).  #!eof exists also in Chez Scheme.  Perhaps other Scheme
implementations have such names (anyone remember #!true and #!false
from R3RS? or was it RRRS).

I wouldn't want to prevent a user from loading a file that starts with
``#!optional'' or ``#!eof'' (or a future addition to the ``#!...''
objects that would have more utility).

Marc