Shebang 和 Groovy

发布于 2024-10-28 07:24:30 字数 124 浏览 1 评论 0原文

是否可以在文件开头声明它应作为 Groovy 脚本执行?

其他脚本语言的示例:

#!/bin/sh
#!/usr/bin/python
#!/usr/bin/perl

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-11-04 07:24:30

这个#!/usr/bin/env groovy
将搜索您的路径寻找 groovy 来执行脚本

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

如何视而不见 2024-11-04 07:24:30

一个常见的技巧是编写一个对多种语言有意义的脚本,也称为“多语言”脚本。

对于 Bash 和 Groovy,这特别简单:

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

println "Hello from Groovy"
  1. 第一行是 shebang (#!),它告诉操作系统将脚本作为常规 shell 脚本运行。
  2. 第二行,当由 shell 执行时,调用 /bin/true 命令(无操作);然后在 PATH 中找到 groovy 可执行文件,并在脚本文件本身 ("$0") 和其他参数上运行它,替换当前的 shell 进程 (exec)
  3. Groovy 将忽略第一行,因为它是一个 shebang;它将忽略第二行,因为它是注释 (//),并将运行脚本的其余部分。

如果您需要更复杂的 shell 部分,也许是为了设置环境变量,或者发现 Groovy 的安装位置,您可以使用不同的技巧:

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

println "Hello from Groovy"
  1. shebang 再次向操作系统发出信号,开始将这些文件作为 shell 脚本执行。
  2. shell 将 '''': 解析为两个空字符串 '' 后跟一个冒号,这是一个无操作。
  3. shell 将逐行执行文件的其余部分,直到找到 execexit
  4. 如果一切正常,shell 将在脚本文件本身 ("$0")
  5. Groovy 将跳过 shebang 行,然后将 '''': 解析为长字符串 '' 的开头',从而跳过所有 shell 命令,然后运行脚本的其余部分。

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.
多情出卖 2024-11-04 07:24:30

根据 this 您可以使用#!/usr/bin/groovy (如果这是它的位置)。您要查找的搜索词是 shebang (这就是第一行的名称)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文