Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shebang and Groovy

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 
like image 859
Nulldevice Avatar asked Mar 29 '11 22:03

Nulldevice


People also ask

What is #! In groovy?

The first line is a shebang ( #! ) that tells the OS to run the script as a regular shell script.

What is a shebang used for?

The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.

Is the shebang necessary?

The shebang is only mandatory for those scripts, which shall be executed by the operating system in the same way as binary executables. If you source in another script, then the shebang is ignored. On the other hand. IF a script is supposed to be sourced, then it is convention to NOT put any shebang at the start.

Why is the shebang line important?

The shebang must be the first line because it is interpreted by the kernel, which looks at the two bytes at the start of an executable file. If these are #! the rest of the line is interpreted as the executable to run and with the script file available to that program.


2 Answers

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

like image 50
jpertino Avatar answered Sep 20 '22 00:09

jpertino


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.
like image 25
Tobia Avatar answered Sep 19 '22 00:09

Tobia