Éxécuter un script lisp avec SBCL en bash

Voilà une petite astuce pour lancer un fichier lisp (ou un .fasl) avec SBCL à partir d’un prompt bash :

sbcl --noinform --load fichier.lisp --eval '(quit)'

C’est vraiment très simple, on peut aussi faire ça ainsi

sbcl --noinform --eval '(load "fichier.lisp")' --eval '(quit)'

Vous l’aurez compris, les –eval sont évalués dans l’ordre dans lequel ils sont passé en argument.
Avec ça, on peut aussi définir une commande pour compiler un fichier :

sbcl --noinform --eval '(compile-file "fichier.lisp")' --eval '(quit)'

Et donc, pour lancer le fichier compilé (un .fasl) :

sbcl --noinform --load fichier.fasl --eval '(quit)'

Évidemment, si on ne spécifie pas le (quit), une fois l’éxécution du fichier terminée, on se retrouve dans la boucle REPL de sbcl.

One Response to “Éxécuter un script lisp avec SBCL en bash”

  1. En pièce jointe, un script qui permet de faire des scripts shells lisp \o/
    Par exemple, pour le Hello, World! :
    ——- helloworld.lisp ——–
    #!/usr/bin/env runlisp

    (format t « Hello, World ! »)
    (print « lulz »)
    ——————————–

    Le fichier runlisp devant être placé dans le PATH (par exemple, le mettre dans ~/bin/ et rajouter ~/bin/ au PATH). Les « scripts lisp » devant être autorisés à l’execution (chmod +x fichier.lisp).

    —– runlisp —–
    #!/bin/sh
    # -*- mode: sh -*-

    # $Id: runlisp,v 1.3 2003/04/22 02:33:53 erik Exp $
    # $Source: /home/cvsd/repo/bin/runlisp,v $

    # A handy script to run Lisp as shellscripts. In the top of your
    # file, put:
    #
    # #!/usr/bin/env runlisp
    #
    # and stick runlisp (this script) somewhere in your path. Then you
    # can have regular-looking shellscripts that are written in Lisp.

    # If you have another Lisp implementation and know what arguments to
    # pass to it for this to work, feel free to add it and send me
    # (erik@nittin.net) the patch.

    if [ -f /usr/bin/sbcl ]; then
    /usr/bin/sbcl –noinform –disable-debugger –eval ‘(set-dispatch-macro-character #\# #\! (lambda (stream bang number) (declare (ignore bang number)) (read-line stream) t))’ –load $1 –eval ‘(quit)’
    elif [ -f /usr/bin/lisp ]; then
    /usr/bin/lisp -quiet -batch -noinit -eval ‘(set-dispatch-macro-character #\# #\! (lambda (stream bang number) (declare (ignore bang number)) (read-line stream) t))’ -load $1 -eval ‘(quit)’
    else
    echo « $0: could not find a Lisp I know how to call »
    fi

Discussion Area - Leave a Comment