Another issue with scripts is that argv[0]
may not be want you
want in some situations. If you run your script directly from a Lisp
invocation such as ‘sbcl --script myscript.lisp’, then it’s
probably okay for argv[0]
to be sbcl
. On the other hand,
suppose you have written a shebang script called myscript
,
starting like this: ‘#!/usr/bin/sbcl --script’. Then, it’s probably
not okay to advertise /usr/bin/sbcl
as the program name
(which it is).
In order to compensate for this problem, the function
make-context
accepts a :progname
initarg which allows you
to override Clon
’s notion of argv[0]
. Currently, the value of
:progname
can be a non-empty string, or :environment
,
meaning to retrieve the value of the __CL_ARGV0
environment
variable (which will be ignored if unset or empty).
Using a non-empty string allows you to hard-wire a pseudo-executable name with regular shebang syntax, as follows.
#!/usr/bin/sbcl --script ... (make-context :progname "myscript") ... |
Of course, this works for as long as your favorite nasty little end-user
doesn’t rename the script in question. A more flexible approach would be
to retrieve the value of $0
from the shell, which is dynamically
computed as the actual script name. Here is a “multiline shebang”
trick to do that.
#| myscript.lisp --- or at least, that was its name originally ;-) export __CL_ARGV0="$0" exec sbcl --script "$0" "$@" |# ... (make-context :progname :environment) ... |
Note how mixing shell and Lisp comments works here. The shell ignores the first line and executes the next 2 ones. The subsequent Lisp invocation ignores the whole first four lines, but gets the actual program name from the environment…