Subject: Re: Lisp as a scripting language?
From: (Rob Warnock)
Date: 1998/11/18
Newsgroups: comp.lang.lisp
Message-ID: <72u482$>
Drake Emko  <> 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))

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

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

	% 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

	% 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
	       #\\# #\\!   ; double the backslashes for 'sh'
	       (defvar script-name \"$0\" )
	       (defvar script-args \"$*\" )
	       (load script-name)"
	exec $dir/ -q -M $dir/lispinit.mem -x "$hack"
	;;;;;;;;;;;; the script's Lisp code starts here ;;;;;;;;;;;;
	(format t "~%Hello world!  Args = ~s~%" script-args)

	% foo bar baz
	;; 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 Warnock, 8L-855
Applied Networking
Silicon Graphics, Inc.		Phone: 650-933-1673
2011 N. Shoreline Blvd.		FAX: 650-964-0811
Mountain View, CA  94043	PP-ASEL-IA