Running Clojure shell scripts in *nix environments

I was recently trying to create a basic piece of Clojure code to play with “real-time” log file parsing by playing with futures. The longer term goal of the experiment is to be able to tail -f a log file pipe that into my Clojure log parser as input.

As I wasn't sure exactly what I would need to be doing, I wanted an easy way to run some code quickly without having to rebuild the jars through Leiningen every time I wanted to try something, in a manner similar to the way I am thinking I will be using it if the experiment succeeds.

I created a file test_input with the following lines:

1 hello
2 test
3 abacus
4 qwerty
5 what
6 dvorak

With this in place, my goal was to be able to run something like cat test_file | parser_concept. After a bit of searching I found the lein-exec plugin for Leiningen, and after very minor setup I was able to start iterating with input piped in from elsewhere.

The first step was to open my profiles.clj file in my ~/.lein directory. I made sure lein-exec was specified in my user plugins as so:

{:user {:plugins [[lein-exec "0.2.1"]
                  ;other plugins for lein
                 ]}}

With this in place I just put the following line at the top of my script.clj file:

#!/usr/bin/env lein exec

I then changed the permissions of script.clj file to make it executable, I was able to run the following and have my code run against the input.

cat test_input | ./script.clj

I will be posting a follow up entry outlining my next step of experimenting with "processing" each line read in as a future.

About these ads

, , , , ,

  1. #1 by jayunit100 on October 23, 2012 - 21:07

    Yayy !!!! This is an awesome trick . Now.. if only the shell script could install lein for you so that it was cross platform :)

  2. #2 by Andreas Steffan (@deas) on October 24, 2012 - 02:02

    I wish the clojure folks would put the general scripting functionality right into the core distribution, so you just write one executable script where we can pull in whatever you need.

    Look at groovy to see an example in action:

    #!/usr/bin/env groovy
    @Grab(‘groupId:artifactId:version’)

    As far as I can see, all we need for clojure would be pomegranate included in the core.

  3. #3 by dribnet on November 2, 2012 - 09:17

    Nice tutorial. A footnote: installing the global plugin via ~/.lein/profiles.clj makes these directions specific to lein 2.

  4. #4 by Willard on November 5, 2012 - 04:17

    I have read several just right stuff here. Definitely value bookmarking for revisiting.
    I surprise how much attempt you place to create the sort of magnificent informative site.

  1. Log File parsing with Futures in Clojure « Proctor It

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 191 other followers

%d bloggers like this: