Using Groovy execute bash scripts

15 / Feb / 2011 by Amit Jain 1 comments

Hi Friends,

Recently I had to execute bash script using groovy on a windows server. Though I could easily make the similar script run on linux, but on windows it just didn’t work as it was unable to recognize the internal DOS commands like cp, rm etc.

On linux the following code worked but not on windows:

[bash]
File script = new File(‘<My_Script_Path>’)
script.getText().execute()
[/bash]

So after trying few options, I found that we can call execute method on script path also apart from calling it directly on commands and which worked for both windows and linux, as given below :

[bash]
"<My_Script_Path>".execute()
[/bash]

To see the output of the script on the console, we can use the following command :

[bash]
println "<My_Script_Path>".execute().text
[/bash]

Hope this helped!

Cheers!
~~Amit Jain~~
amit@intelligrape.com
http://www.tothenew.com/blog/

FOUND THIS USEFUL? SHARE IT

comments (1 “Using Groovy execute bash scripts”)

Leave a Reply

Your email address will not be published. Required fields are marked *