Shebang 和 Groovy
是否可以在文件开头声明它应作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个
#!/usr/bin/env groovy
将搜索您的路径寻找 groovy 来执行脚本
This one
#!/usr/bin/env groovy
will search your path looking for groovy to execute the script
一个常见的技巧是编写一个对多种语言有意义的脚本,也称为“多语言”脚本。
对于 Bash 和 Groovy,这特别简单:
#!
),它告诉操作系统将脚本作为常规 shell 脚本运行。/bin/true
命令(无操作);然后在 PATH 中找到 groovy 可执行文件,并在脚本文件本身 ("$0"
) 和其他参数上运行它,替换当前的 shell 进程 (exec
)//
),并将运行脚本的其余部分。如果您需要更复杂的 shell 部分,也许是为了设置环境变量,或者发现 Groovy 的安装位置,您可以使用不同的技巧:
'''':
解析为两个空字符串''
后跟一个冒号,这是一个无操作。exec
或exit
"$0"
)'''':
解析为长字符串'' 的开头'
,从而跳过所有 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:
#!
) that tells the OS to run the script as a regular shell script./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
)//
) 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:
'''':
as two empty strings''
followed by a colon, which is a no-op.exec
or anexit
"$0"
)'''':
as the beginning of a long string'''
, thus skipping all the shell commands, and then run the rest of the script.根据 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).