Next: , Up: Initialization Examples


2.4.4.1 Unix-style Command Line Protocol

Standard Unix tools that are interpreters follow a common command line protocol that is necessary to work with “shebang scripts”. SBCL doesn't do this by default, but adding the following snippet to an initialization file does the trick:

     ;;; If the first user-processable command-line argument is a filename,
     ;;; disable the debugger, load the file handling shebang-line and quit.
     (let ((script (and (second *posix-argv*)
                        (probe-file (second *posix-argv*)))))
        (when script
           ;; Handle shebang-line
           (set-dispatch-macro-character #\# #\!
                                         (lambda (stream char arg)
                                            (declare (ignore char arg))
                                            (read-line stream)))
           ;; Disable debugger
           (setf *invoke-debugger-hook*
                 (lambda (condition hook)
                   (declare (ignore hook))
                   ;; Uncomment to get backtraces on errors
                   ;; (sb-debug:backtrace 20)
                   (format *error-output* "Error: ~A~%" condition)
                   (quit)))
           (load script)
           (quit)))

Example file (hello.lisp):

     #!/usr/local/bin/sbcl --noinform
     (write-line "Hello, World!")

Usage examples:

     $ ./hello.lisp
     Hello, World!
     $ sbcl hello.lisp
     This is SBCL 0.8.13.70, an implementation of ANSI Common Lisp.
     More information about SBCL is available at <http://www.sbcl.org/>.
     
     SBCL is free software, provided as is, with absolutely no warranty.
     It is mostly in the public domain; some portions are provided under
     BSD-style licenses.  See the CREDITS and COPYING files in the
     distribution for more information.
     Hello, World!