Run Unix cat command in Groovy?

Hello

I would like to execute something like cat / path / to / file1 / path / to / file2> / path / to / file3 from Groovy program. I tried "cat / path / to / file1 / path / to / file2> / path / to / file3 .execute () but that did not work.

After some searching, I read about sh -c. So I tried "sh -c cat / path / to / file1 / path / to / file2> / path / to / file3" .execute (), but that didn't work either.

Do you have any suggestions?

+3
source share
2 answers

I believe that you need to use the method List.execute()to run it in the command console, that is:

[ 'sh', '-c', 'cat file1 file2 > file3' ].execute()

Or you can do it groovy way

new File( 'file3' ).with { f ->
  [ 'file1', 'file2' ].each { f << new File( it ).asWritable() }
}
+4
source

Perhaps you need to provide the full path to the executable? "/bin/sh -c '/bin/cat file1 file2 >file3'".execute()

+1
source

All Articles