从linux中的groovy执行bash脚本

时间:2012-08-14 16:37:18

标签: linux groovy echo

我的服务器上有一个bash脚本,它从github获取master分支,构建它并将工件部署到tomcat。我决定编写一个github web URL hook,它会调用这个sh。当我的服务器运行Java时,我正在使用groovy。我在test.sh中写了一个测试脚本/home/madhead/scripts

echo "SHELL"
touch /home/madhead/test_`date +%d_%m_%Y_%H_%M_%S` # To see if script is actually called

SCRIPTS中的环境变量/home/madhead/scripts设置为/home/madhead/.bashrc。在我的groovlet中我有

println "GROOVY"
println '$SCRIPTS/test.sh'.execute().text
println `whoami'.execute().text` //Prints madhead
println `env'.execute().text` //Prints all environment variables for madhead, SHELL is /bin/bash and SCRIPTS is /home/madhead/scripts in this output.

“GROOVY”在html中打印,但是当我调用groovlet时没有“SHELL”并且没有创建测试文件。因此,脚本不会被调用。我试过了

println '/home/madhead/scripts/test.sh'.execute().text
在groovlet中

没有效果。我如何从groovy / java调用bash脚本?此外,println 'echo test'.execute().text将测试打印到html,但println 'echo $SCRIPTS'.execute().text不打印任何内容。为什么呢?

1 个答案:

答案 0 :(得分:2)

可能需要获取环境变量:

def env = System.getenv()
println env.SCRIPTS

在我的盒子里,这家伙工作:

groovy -e ' def env = System.getenv(); println env.JAVA_HOME '