Subject: Re: Lisp as a scripting language?
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1998/11/18
Newsgroups: comp.lang.lisp
Message-ID: <72u482$av9kq@fido.engr.sgi.com>
Drake Emko  <drake@lynxus.com> wrote:
+---------------
| However, I have had trouble finding information on how to use lisp in a
| scripting context...  eg. code that starts like:
|         #!/usr/local/bin/lisp
| but I have only found lisp code written for running within the
| interpreter.  Can I write a script that invokes lisp as a command
| interpreter, like a python or perl script?  I tried doing that, but my
| lisp implementation (CMU CL) just ignored the body of the script.
+---------------

It's extremely implementation-dependent how you do this. Here's how you
do it for several versions of Scheme. Note the various options required to
treat the script as a file to be loaded, and the various ways of getting
at the script's arguments:

Elk:	#!/usr/local/bin/elk -l 
	(display "Hello, world! Args = ") 
	(display (command-line-args))
	(newline)

	% foo.elk bar baz
	Hello, world! Args = (bar baz)
	% 

MzScheme:
	% cat foo.mz
	#!/usr/local/bin/mzscheme -r
	(display "Hello, world! Args = ") 
	(display argv)		; note: "argv" is a vector
	(newline)

	% foo.mz bar baz
	Hello, world! Args = #(bar baz)
	% 

Scsh:	% cat foo.scsh
	#!/usr/local/bin/scsh -s
	!#
	(display "Hello, world! Args = ")
	(display (command-line))	; includes the script name
	(newline)

	% foo.scsh bar baz
	Hello, world! Args = (foo.scsh bar baz)
	% 

SIOD:	% cat foo.siod
	#!/usr/local/bin/siod -v0
	(writes nil "Hello, world!  Args = ")
	(print *args*) ; includes the interpreter, its args, & the script name

	% foo.siod bar baz
	Hello, world!  Args = ("/usr/local/bin/siod" "-v0" "foo" "bar" "baz")
	% 


It requires that the Lisp or Scheme reader know to ignore the "#!" line,
which most Schemes seem to. ("Scsh" does it by defining "#!....!#" to be
another form of "#|...|#, whereas the others just special-case the first
line of any LOADed file.)

As distributed, CLISP doesn't handle this [AFAIK], but you can either
hack on the source code of CLISP a little, or fake up a style sorta
like "scsh" with the following kludge:

	% cat foo
	#!/bin/sh
	dir=/usr/local/lib/clisp
	hack="(set-dispatch-macro-character
	       #\\# #\\!   ; double the backslashes for 'sh'
	       #'system::comment-reader
	       *readtable*)
	       (defvar script-name \"$0\" )
	       (defvar script-args \"$*\" )
	       (load script-name)"
	exec $dir/lisp.run -q -M $dir/lispinit.mem -x "$hack"
	!#
	;;;;;;;;;;;; the script's Lisp code starts here ;;;;;;;;;;;;
	(format t "~%Hello world!  Args = ~s~%" script-args)
	(exit)

	% foo bar baz
	T
	SCRIPT-NAME
	SCRIPT-ARGS
	;; Loading file foo ...
	Hello world!  Args = "bar baz"
	% 

Unfortunately, I haven't found any way (without hacking the source) to
suppress the first three values printed to stdout or the "Loading..."
message.  Maybe other CLs do it better...?


-Rob

-----
Rob Warnock, 8L-855		rpw3@sgi.com
Applied Networking		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
2011 N. Shoreline Blvd.		FAX: 650-964-0811
Mountain View, CA  94043	PP-ASEL-IA