0

im creating a groovy script for executing sql scripts from the docker container. This is my command that i want to execute

docker exec -i wienerberger_generalshale_com mysql -u root -proot -r < 001_CategorySamples.sql  generalshale_website

And it works when i execute it from terminal, but when i call my groovy script which execute that line too, i get a mysql help as result, like if the command is not right.

This is how i execute the command from groovy:

def command =  "docker exec -i wienerberger_generalshale_com mysql -u root -proot -r < 001_CategorySamples.sql  generalshale_website"
def proc = command.execute();
def outputStream = new StringBuffer();
proc.waitForProcessOutput(outputStream, System.err);
println(outputStream .toString());

Does anybody know why the result is not the same or why the command is not recognized?

filipvvv
  • 1
  • 1
  • 1
    You can not use "shellisms" like `<` with execute, as it is just an execute. Either use `['sh', '-c', '...'].execute()` or use the piping as described in end of http://groovy-lang.org/groovy-dev-kit.html#process-management – cfrick Aug 26 '18 at 12:48

1 Answers1

2

The redirection from the sql file is handled by your operating system shell when invoking from the terminal directly not by the command you are executing (docker in your case). You have to handle that yourself. Something like:

def infile = '001_CategorySamples.sql'
def command = 'docker exec -i wienerberger_generalshale_com mysql -u root -proot -r generalshale_website'
def proc = command.execute()
Thread.start{
    def writer = new PrintWriter(new BufferedOutputStream(proc.out))
    writer << new File(infile).text
    writer.close()
}
def outputStream = new StringBuffer()
proc.waitForProcessOutput(outputStream, System.err)
println outputStream

You can find similar questions asked for Java:

Or see this PLEAC (Perl cookbook translations) page for examples for some similar Groovy scenarios:

Paul King
  • 627
  • 4
  • 6