3
votes

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

2 Answers

6
votes

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.

-1
votes

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.