Does Chicken Scheme have an equivalent to Perl's $0?

1.2k views Asked by At

How can I reliably get the script name in Chicken Scheme?

It seems that -ss eats up the script name, so it's not visible unless I use dot slash to run my scripts.

scriptedmain.scm:

#!/usr/bin/env csi -q

(display (command-line-arguments))
(display "\n")
(exit)

Trace:

$ ./scriptedmain.scm 
(-q ./scriptedmain.scm)
wonko:Desktop andrew$ csi -ss scriptedmain.scm 
()
3

There are 3 answers

6
Imran Rafique On BEST ANSWER

This is a late response, so may not be of use to the original poster. But to any others who may come across this question, the simple answer is to use the parameter:

(program-name)

This should return the correct name for all situations. Docs here.

0
mcandre On

scriptedmain.scm will run (main) and print the program name in the following cases:

Run from the interpreter:

csi -ss scriptedmain.scm

Run from the interpreter using shebangs:

./scriptedmain.scm

Compiled:

csc -o scriptedmain scriptedmain.scm
./scriptedmain

Added to GitHub.

#!/bin/sh
#|
exec csi -ss $0 ${1+"$@"}
exit
|#

(define (main)
    (display (format "Program: ~a\n" (program-name)))
    (exit))

(if (not (equal? (program-name) "csi"))
    (main))
2
knivil On

(argv) should do the job. Example:

#!/usr/local/bin/csi -script

(display (argv)) (newline) (exit)

prints (/usr/local/bin/csi -script ./test.scm)