madhead
madhead

Reputation: 33412

Execute bash script from groovy in linux

I have a bash script on my server which fetches master branch from github, builds it and deploys artifacts to the tomcat. I decided to write a github web URL hook which will call this sh. As my server is running Java, I'm using groovy. I writed a test script test.sh in /home/madhead/scripts:

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

Environment variable SCRIPTS is set to /home/madhead/scripts in /home/madhead/.bashrc. In my groovlet I have

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" is printend in html, but no "SHELL" and no test files are created when i calling groovlet. So, the script is not called. I tried

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

in groovlet with no effect. How do i call bash script from groovy/java? Also, println 'echo test'.execute().text prints test to html, but println 'echo $SCRIPTS'.execute().text does not print anything. Why?

Upvotes: 1

Views: 4930

Answers (1)

Will
Will

Reputation: 14529

Maybe to get the environment variable you need to:

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

In my box this guy works:

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

Upvotes: 2

Related Questions