May I suggest instead compiling your program as an uberjar? A jar can be executed by name just as you would your wrapper script, but this way you don't need the extra wrapper.
Download the latest version of leiningen and create a project $ lein new foo
and then go into the directory. When you are in the process of developing your code you'll probably want to use a repl ($ lein repl
), or call $ lein run
to run it from the command line.
Edit src/foo/core.clj: add a gen-class for AOT compilation, and a main function.
(ns foo.core
(:gen-class))
(defn -main [& [a]]
(println (format "Hello, %s World!" a)))
Edit project.clj and make this the main class:
(defproject foo "0.0.1"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.4.0"]]
:main foo.core)
Now compile the uberjar and make it executable:
$ lein do clean, compile, uberjar ; chmod +x ./target/foo-0.0.1-standalone.jar
The executable jar is in the target/ directory, which you can now call by name like any other executable:
$ cd target
$ mv foo-0.0.1-standalone.jar foo
$ ./foo crazy
Hello, crazy World!
$ ./foo
Hello, null World!
Other Approaches
Leiningen w/ shebang
Use leiningen shebang-style http://charsequence.blogspot.com/2012/04/scripting-clojure-with-leiningen-2.html
ClojureScript + V8 -> js w/ shebang
Clojure is not really the best choice for command-line scripting, because of JVM warm up time. ClojureScript + V8 engine was put forward (at the inaugural announcement of ClojureScript by Rich Hickey) as a better solution for scripting. Here is blog article with a detailed example http://mmcgrana.github.com/2011/09/clojurescript-nodejs.html And here is a StackOverflow answer showing how to use node in a shebang: Is it possible to run Node.js scripts without invoking `node`?
However since your goal seems to be to have no compile step, you may want to write a script that you will use in your shebang that builds the js from cljs before running it. In this case, you will again have the same JVM start-up time issue, so you may be better off with Debasish's simpler approach linked above.