Shebang and Groovy

19,107

Solution 1

This one #!/usr/bin/env groovy
will search your path looking for groovy to execute the script

Solution 2

A common trick is to write a script that has meaning in more than one language, also known as a "polyglot" script.

In the case of Bash and Groovy, this is particularly easy:

#!/bin/sh
//bin/true; exec groovy -cp .. "$0"

println "Hello from Groovy"
  1. The first line is a shebang (#!) that tells the OS to run the script as a regular shell script.
  2. The second line, when executed by the shell, invokes the /bin/true command (a no-op); then finds the groovy executable in the PATH and runs it on the script file itself ("$0") plus additional arguments, replacing the current shell process (exec)
  3. Groovy will ignore the first line, because it's a shebang; it will ignore the second line because it's a comment (//) and will run the rest of the script.

If you need a more elaborate shell part, maybe to set up environment variables, or discover where Groovy is installed, you can use a different trick:

#!/bin/sh
'''':
echo Hello from Shell
exec groovy -cp .. "$0"
'''

println "Hello from Groovy"
  1. Again, the shebang signals the OS to start executing this files as a shell script.
  2. The shell parses '''': as two empty strings '' followed by a colon, which is a no-op.
  3. The shell will execute the rest of the file, line by line, until it find an exec or an exit
  4. If everything is ok, the shell will run the Groovy command on the script file itself ("$0")
  5. Groovy will skip the shebang line, then it will parse '''': as the beginning of a long string ''', thus skipping all the shell commands, and then run the rest of the script.

Solution 3

According to this you can use #!/usr/bin/groovy (if that's its location). The search term you are looking for is shebang (which is what that first line is called).

Share:
19,107

Related videos on Youtube

Nulldevice
Author by

Nulldevice

twitter.com/russinkovsky

Updated on June 06, 2020

Comments

  • Nulldevice
    Nulldevice almost 4 years

    Is it possible to declare at the start of a file that it should be executed as a Groovy script?

    Examples for other scripting languages:

    #!/bin/sh
    #!/usr/bin/python
    #!/usr/bin/perl
    
    • Nulldevice
      Nulldevice about 13 years
      It's easy when you know that #! is called "shebang", as you could see from comments I did not know about that..
    • glenn jackman
      glenn jackman about 13 years
      Note that the first word on the shebang line has to be a binary executable. If it happens that your "groovy" program is a shell script, you have to use the #!/usr/bin/env groovy form.
    • Bogdan Calmac
      Bogdan Calmac about 10 years
      The funny thing is that googling it returns exactly this page. stack overflow is actually the best resource, even for simple questions like this one. Other people will come directly here and get an authoritative answer rather than browsing through different blog posts.
  • Nulldevice
    Nulldevice about 13 years
    thank! It was known to me that combination #! has special name, but I failed to remember it.
  • Patrick
    Patrick over 12 years
    A limitation is that you can't give groovy any arguments (like classpath) when using a shebang on linux. See this answer.
  • mfortner
    mfortner about 9 years
    If it helps you remember, the starting symbols for the line are a "sharp" # and an interrobang !, hence the term "shebang".
  • M. Justin
    M. Justin about 7 years
    @mfortner A good mnemonic, but the exclamation point is a bang, not an interrobang. An interrobang is the combination exclamation point/question mark symbol (‽).
  • Andreas M. Oberheim
    Andreas M. Oberheim over 5 years
    I tried all other solutions. This is the only one that worked inside a Windows 7 git bash installation on a 64-bit machine with very restricted rights
  • Chiramisu
    Chiramisu over 4 years
    It seems to me that you can't go wrong by sticking with #!/usr/bin/env <environment>, as is the case with #!/usr/bin/env python as well. ;)