Node:The Top of a Script File, Next:Scripting Examples, Up:Guile Scripts
The first line of a Guile script must tell the operating system to use Guile to evaluate the script, and then tell Guile how to go about doing that. Here is the simplest case:
#!
.
The operating system interprets this to mean that the rest of the line
is the name of an executable that can interpret the script. Guile,
however, interprets these characters as the beginning of a multi-line
comment, terminated by the characters !#
on a line by themselves.
(This is an extension to the syntax described in R5RS, added to support
shell scripts.)
/usr/local/bin/guile
.
-s
. This switch tells Guile to run a
script, instead of soliciting the user for input from the terminal.
There are more elaborate things one can do here; see The Meta Switch.
!#
-- just like the top of the file, but reversed. The
operating system never reads this far, but Guile treats this as the end
of the comment begun on the first line by the #!
characters.
Guile reads the program, evaluating expressions in the order that they appear. Upon reaching the end of the file, Guile exits.
The function command-line
returns the name of the script file and
any command-line arguments passed by the user, as a list of strings.
For example, consider the following script file:
#!/usr/local/bin/guile -s !# (write (command-line)) (newline)
If you put that text in a file called foo
in the current
directory, then you could make it executable and try it out like this:
$ chmod a+x foo $ ./foo ("./foo") $ ./foo bar baz ("./foo" "bar" "baz") $
As another example, here is a simple replacement for the POSIX
echo
command:
#!/usr/local/bin/guile -s !# (for-each (lambda (s) (display s) (display " ")) (cdr (command-line))) (newline)
command-line | Scheme Procedure |
program-arguments | Scheme Procedure |
Return a list of the command-line arguments passed to the currently
running program. If the program invoked Guile with the -s ,
-c or -- switches, these procedures ignore everything up
to and including those switches.
|