获取当前在 Steel Bank Common Lisp 中执行的脚本的路径

Get the path of the script that is currently executing in Steel Bank Common Lisp

我正在从系统中的不同位置调用 CL 脚本。如何获取当前执行脚本的文件路径?

例如脚本源文件位于/home/user/project/source/目录下。脚本以下列方式从 /home/user/ 目录执行:

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

无论调用方位置如何,脚本都应该知道它位于 /home/user/project/source/ 目录中。

我试过使用 *default-pathname-defaults* 变量,但以下命令显示了调用脚本的目录:

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

环境:SBCL 1.4.5.debian Ubuntu 18.04.

我在浏览不同的问题时偶然发现了 one possible answer

正如@Andrei 所指出的,可以读取使用以下表达式调用的脚本的完整路径名:

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

由于此解决方案依赖于 $_ 环境变量和 bash shell,因此它可能无法移植,正如 @DmitryGrigoryev 在 this answer at unix.stackexchange.com.[=14 中指出的那样=]

*load-truename**compile-file-truename*变量在加载或编译时绑定到cl:load加载或cl:compile-file编译的文件的真实名称,分别

在您的情况下,*load-truename* 是要使用的东西。它将为脚本提供完整的绝对路径名。