简体   繁体   English

从命令行运行Groovy脚本

[英]Running Groovy script from the command line

When I did which groovy , I got the below output: 当我执行which groovy ,我得到以下输出:

/usr/local/bin/groovy

So I went ahead and created a helloworld.groovy with the below content 因此,我继续创建了helloworld.groovy ,其内容如下

#!/usr/local/bin/groovy
println "hello world"

After that I did chmod +x helloworld.groovy and attempted to run the file with ./hellworld.groovy and sadly, I got this error ./helloworld.groovy: line 2: print: command not found 之后,我执行了chmod +x helloworld.groovy并尝试使用./hellworld.groovy运行文件,可悲的是,我遇到了此错误./helloworld.groovy: line 2: print: command not found

I could get rid of the error by changing to 我可以通过更改为摆脱错误

#!/usr/bin/env groovy
println "hello world"

Why would the first method cause the error? 为什么第一种方法会导致错误?

您需要像这样运行脚本:

groovy helloworld.groovy

It will work on Linux kernel 2.6.28 (confirmed on 4.9.x). 它将在Linux内核2.6.28(在4.9.x上确认)上工作。 It won't work on FreeBSD and other Unix flavors. 它不适用于FreeBSD和其他Unix版本。

Your /usr/local/bin/groovy is a shell script wrapping the Java runtime running Groovy. 您的/usr/local/bin/groovy是一个外壳脚本,其中包含运行Groovy的Java运行时。

See the Interpreter Scripts section of EXECVE(2) and EXECVE(2) . 请参阅EXECVE(2)EXECVE(2)解释器脚本部分。

#!/bin/sh
sed '1,2d' "$0"|$(which groovy) /dev/stdin; exit;

println("hello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM