groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maarten Boekhold <boekh...@gmx.com>
Subject Re: Trick: using groovy script as a "shell script" on UNIX
Date Tue, 02 Jun 2015 09:39:25 GMT
Hi

Because you cannot add any command line parameters to that. The way a 
UNIX system uses the shebang line is that it has (1) a program/shell to 
run, (2) followed by a /single argument/. So if you write:

    #!/usr/bin/env groovy -Dmy.opt1=foo

    println System.env["my.opt1"]


This will fail, because it will attempt to run: /usr/bin/env "groovy 
-Dmy.opt1=foo", and there is no such program. In fact, if I try this on 
my RHEL 6.6 system, it just hangs. Doesn't even return to the shell 
prompt, nothing happens. Not really sure why...

If you specify the full path to groovy like:

    #!/home/maarten/groovy-2.4.3/bin/groovy -Dmy.opt1=foo

    println System.env["my.opt1"]

you get "null".

Maarten

On 2015-06-02 13:30, Cédric Champeau wrote:
> Hi!
>
> I am curious why you don't directly use the shebang line?
>
> http://docs.groovy-lang.org/docs/latest/html/documentation/core-syntax.html#_shebang_line
>
>
> On 06/02/2015 11:25 AM, Maarten Boekhold wrote:
>> Hi all,
>>
>> I recently discovered a neat trick to create a standalone groovy 
>> script on UNIX that you can run as if it was a shell/bash script. The 
>> first part of the trick I got from the following StackOverflow post:
>>
>> http://stackoverflow.com/questions/306139/how-do-i-include-jars-in-a-groovy-script/30503877#30503877
>>
>> You can "embed" a groovy script inside a bash script as follows:
>>
>>     #!/bin/bash
>>     //usr/bin/env /path/to/groovy "$0" @0; exit $?
>>
>>     println "My groovy script"
>>
>> This launches a bash shell that runs the groovy interpreter /on this 
>> same file/ ("$0"), because "//usr/bin/env" is just equivalent to 
>> "/usr/bin/env" (eg the double leading slash is collapsed to a single 
>> slash by bash). Groovy ignores the first line if it starts with a #, 
>> and the second line starts with // which is a comment in groovy.
>>
>> If you need to add classpath entries or java properties or any other 
>> command line options to groovy, you can insert them on that second 
>> line as well of course, but that can generate quite a long and 
>> unreadable line if you have a lot of entries. However you can use the 
>> same "// trick" to set environment variables /before/ you launch groovy:
>>
>>     #!/bin/bash
>>     //usr/bin/true && export CLASSPATH=....
>>     //usr/bin/true && export OPTS="-Dmy.opt1=foo -Dmy.opt2=bar"
>>     //usr/bin/true && export OPTS="$OPTS -Dmy.opt3=foobar"
>>     //path/to/groovy $OPTS "$0" @0; exit $?
>>
>>     println System.env[my.opt1]
>>
>>
>> Note that on the line starting groovy you don't even need to prefix 
>> it with //usr/bin/env as long as you use a fully qualified path to 
>> the groovy interpreter. If you want bash to find groovy in your PATH 
>> however you do need that //usr/bin/env prefix.
>>
>> Neat eh? Maybe we can include this in the documentation somewhere?
>>
>> Maarten
>
>
> -- 
> Cédric Champeau
> Groovy language developer
> http://twitter.com/CedricChampeau
> http://melix.github.io/blog
>


Mime
View raw message