skip to Main Content

I’m calling a CL script from various places in the system. How do I get the file path of the script that is currently executed?

For example, the script source file is located in the /home/user/project/source/ directory. The script is being executed from the /home/user/ directory in the following manner:

user@machine:~$ ./project/source/script.lsp

Regardless of the callers location, script should know that it’s located in the /home/user/project/source/ directory.

I’ve tried using the *default-pathname-defaults* variable, but the following command shows the directory from which the script was called:

(format t "Pathname: ~S~&" *default-pathname-defaults*)

Environment: SBCL 1.4.5.debian on Ubuntu 18.04.

2

Answers


  1. Chosen as BEST ANSWER

    I stumbled upon one possible answer while browsing through different question.

    As @Andrei has noted, it's possible to read the full pathname of the script which is invoked by using the following expression:

    ; truename function expands relative path to the script stored in $_ variable
    (truename (sb-ext:posix-getenv "_"))
    

    Since this solution is relying on $_ environment variable and bash shell, it may not be portable as @DmitryGrigoryev has noted in this answer at unix.stackexchange.com.


  2. The *load-truename* and *compile-file-truename* variables are bound to the truename of the file being loaded with cl:load or compiled with cl:compile-file at the time of loading or compilation, respectively.

    In your case, *load-truename* is the thing to use. It will give a full, absolute pathname to the script.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search