1

I'm using pre-build binary exe of tcsh on Windows.

I'm trying to write some clojure script. like this:

#!/usr/bin/env java clojure.main

(prn "Hello World!\n")

first I try to direct run it in tcsh:

# cat test
#!/usr/bin/env java clojure.main

(prn "Hello World\n")
# ./test
#

no lucky: a "choose correct program to open this file" window occurs, but not execute it.

I'm trying to add +x mod to it:

# chmod +x test
# ls-F -l test
-rwxr-xr-x    1 SW       Administ       59 Oct  9 01:25 test*
# ./test

still the same :(

the question is:

  • Does the pre-build version of tcsh on Windows support hashbang?
  • If so, how to make it execute the file with hashbang?

Thx for a lot :-)

Kevin Brown
  • 36,829
  • 37
  • 188
  • 225

2 Answers2

0

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.

Community
  • 1
  • 1
rplevy
  • 5,163
  • 3
  • 29
  • 30
  • But a jar can not modify easily. My original idea is using clojure as a script language, such as using its source code as a single executable file. but thank you all the same as to create a clojure project and to compile it to jar is another subject I want to know. Thank you :-) – Xavier Wang Oct 09 '12 at 10:12
0

A bit late to this party :-).

To answer the direct questions about hashbang support on Windows: the prebuilt tcsh on Widows has a TCSHSUBSTHB environment variable that can be set. From the README.NT it is described as:

Specifies mappings for hashbang emulation. Should be ';'-separated pairs of blank-separated mappings. For example,

   setenv TCSHSUBSTHB "/usr/local/bin/perl c:/bin/perl.exe;"

will substitute #!c:/bin/perl.exe for scripts which have #!/usr/local/bin/perl at the top.

The final ';' MUST be included.

For example in my .tcshc I have

setenv TCSHSUBSTHB "/usr/bin/tclsh j:/Tcl/bin/tclsh84.exe;"

If you can get away with putting #!/usr/bin/java at the top of this script then you can use TCSHSUBSTHB to map to your Windows java.exe.

Another way to work around this is to use Cygwin's tcsh, which, being more Unix like, supports hashbang.

Andrew Stein
  • 11,807
  • 4
  • 30
  • 43